N
N
Nikolai2018-11-08 02:45:37
Drupal
Nikolai, 2018-11-08 02:45:37

How to prevent editing a field if it is already filled?

I've been suffering for a couple of days now. It is necessary to forbid editing the "Link to term" field if the field already has a value. But for some reason the module does not want to work. No way. No errors in the logs, but that's all.
Here is the module itself:

function field_acces_if_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$field_name = 'field_user_company_name'; // Машинное имя поля
$langcode = LANGUAGE_NONE; // Код языковой версии
if (isset($form[$field_name][$langcode])) {
  $items = element_children($form[$field_name][$langcode]);
  foreach ($items as $i) {
    // Если значение поля заполнено
    if ($form[$field_name][$langcode][$i]['value']['#default_value']) {
      $form[$field_name]['#disabled'] = TRUE; // Выключаем поле
      break; // И выходим из цикла
    }
  }
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andead, 2018-11-08
@andead

In the simplest case:
But it is not certain that it will be LANGUAGE_NONE and that the element has [$i] and ['value']. See what's in the array, disable the same element you're checking for #default_value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question