You are here

function auto_entitylabel_form_alter in Automatic Entity Label 7

Same name and namespace in other branches
  1. 8.3 auto_entitylabel.module \auto_entitylabel_form_alter()
  2. 8 auto_entitylabel.module \auto_entitylabel_form_alter()
  3. 8.2 auto_entitylabel.module \auto_entitylabel_form_alter()

Implements hook_form_alter().

1 call to auto_entitylabel_form_alter()
auto_entitylabel_field_attach_form in ./auto_entitylabel.module
Implements hook_field_attach_form().

File

./auto_entitylabel.module, line 115
Allows hiding of entity label fields and automatic label creation.

Code

function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#entity_type']) && isset($form['#bundle']) && empty($form['#auto_entitylabel_processed'])) {
    $info = entity_get_info($form['#entity_type']);

    // Exit if there is no label field, e.g. users only have a "label callback".
    if (!isset($info['entity keys']['label'])) {
      return;
    }
    $key = $form['#entity_type'] . '_' . $form['#bundle'];
    $title = $info['entity keys']['label'];
    $form['#auto_entitylabel_processed'] = TRUE;

    // Integration with the title module.
    $replacement = FALSE;
    if (module_exists('title') && title_field_replacement_enabled($form['#entity_type'], $form['#bundle'], $title)) {
      $title = $info['field replacement'][$title]['instance']['field_name'];
      $replacement = TRUE;
    }
    if (auto_entitylabel_get_setting($key) == AUTO_ENTITYLABEL_ENABLED) {

      // We will autogenerate the title later, just hide the title field in the
      // meanwhile.
      if ($replacement && isset($form[$title])) {
        $form[$title][$form[$title]['#language']][0]['value']['#value'] = AUTO_ENTITYLABEL_PLACEHOLDER;
        $form[$title][$form[$title]['#language']][0]['value']['#type'] = 'value';
        $form[$title][$form[$title]['#language']][0]['value']['#required'] = FALSE;
      }
      else {
        $form[$title]['#value'] = AUTO_ENTITYLABEL_PLACEHOLDER;
        $form[$title]['#type'] = 'value';
        $form[$title]['#required'] = FALSE;
      }
    }
    elseif (auto_entitylabel_get_setting($key) == AUTO_ENTITYLABEL_OPTIONAL) {
      if ($replacement && isset($form[$title])) {
        $form[$title][$form[$title]['#language']][0]['value']['#required'] = FALSE;
      }
      else {
        $form[$title]['#required'] = FALSE;
      }
    }
  }
}