You are here

function token_form_field_config_edit_form_alter in Token 8

Implements hook_form_FORM_ID_alter().

File

./token.module, line 305
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
  $field_config = $form_state
    ->getFormObject()
    ->getEntity();
  $field_storage = $field_config
    ->getFieldStorageDefinition();
  if ($field_storage
    ->isLocked()) {
    return;
  }
  $field_type = $field_storage
    ->getType();
  if (($field_type == 'file' || $field_type == 'image') && isset($form['settings']['file_directory'])) {

    // GAH! We can only support global tokens in the upload file directory path.
    $form['settings']['file_directory']['#element_validate'][] = 'token_element_validate';

    // Date support needs to be implicitly added, as while technically it's not
    // a global token, it is a not only used but is the default value.
    // https://www.drupal.org/node/2642160
    $form['settings']['file_directory'] += [
      '#token_types' => [
        'date',
      ],
    ];
    $form['settings']['file_directory']['#description'] .= ' ' . t('This field supports tokens.');
  }

  // Note that the description is tokenized via token_field_widget_form_alter().
  $form['description']['#element_validate'][] = 'token_element_validate';
  $form['description'] += [
    '#token_types' => [],
  ];
  $form['token_tree'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [],
    '#weight' => $form['description']['#weight'] + 0.5,
  ];
}