Score:-2

Is there a way to allow entering a value in a node's field if inserting a new node, but make the field disabled if updating that same node?

in flag

I'm using Drupal 7 and I have a standard text field in my Content Type. If the user with role "Standard User" is adding a new node then they should be able to set the Status field text value. However, if the node is already created and they are editing/updating that same node, it should be grayed-out (disabled). Is there a way to do this?

Kevin avatar
in flag
Other than do it with Form API and hook_form_alter?
MrSnrub avatar
in flag
can you show me an example?
Score:0
in flag
function my_module_form_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form_state['node'];

  if ($form_id == "my_content_type_node_form") {

    global $user;

    if (isset($user)) {

      $user_has_standard_user_role = FALSE;

      // Get role information about the user.
      $user_roles = array();
      foreach ($user->roles as $key => $role) {
        $user_roles[$key] = $role;
      }

      foreach ($user_roles as $key => $user_role) {
        if ($user_role == "Standard User") {
          $user_has_standard_user_role = TRUE;
        }
      } // end iterate through user roles

      if ($user_has_standard_user_role) {

        // If this is a new node, let the user edit the field.
        if (!isset($node->nid) || isset($node->is_new)) {
          $form['field_status_field']['und']['#disabled'] = FALSE;
        }

        // If this is not a new node, then disable the field.
        else {
          $form['field_status_field']['und']['#disabled'] = TRUE;
        }
      } // end if user has Standard User role.

      // User is not Standard User, so disable the field.
      else { 
        $form['field_status_field']['und']['#disabled'] = TRUE;
      }
    } // end if user is set
  } // end if correct form ID
 } // end function
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.