You are here

public function AvatarKitFormAlter::fieldConfigEditForm in Avatar Kit 8.2

Implements hook_form_FORM_ID_alter().

FORM_ID: 'field_config_edit_form'.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Overrides AvatarKitFormAlterInterface::fieldConfigEditForm

See also

\avatars_form_field_config_edit_form_alter()

File

src/AvatarKitFormAlter.php, line 33

Class

AvatarKitFormAlter
Drupal form alters.

Namespace

Drupal\avatars

Code

public function fieldConfigEditForm(array &$form, FormStateInterface $form_state) : void {
  $field_config = $this
    ->getFieldConfig($form_state);
  $avatar_field_types = [
    'file',
    'image',
  ];
  if (!in_array($field_config
    ->getType(), $avatar_field_types)) {

    // Only add our form alters if this is a valid avatar field target.
    return;
  }
  $target_entity_type_id = $field_config
    ->getTargetEntityTypeId();
  $targetBundle = $field_config
    ->getTargetBundle();

  // Only show this form if this type is in the active map.
  $entityMap = AvatarKitEntityMap::load($target_entity_type_id . '.' . $targetBundle . '.' . 'default');
  $entityMapFieldName = $entityMap ? $entityMap
    ->getFieldName() : NULL;
  if ($entityMapFieldName != $field_config
    ->getName()) {
    return;
  }
  $form['avatars'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Avatars'),
    '#tree' => TRUE,
    '#open' => TRUE,
    '#weight' => 50,
  ];
  $third_party = $field_config
    ->getThirdPartySettings('avatars');
  $form['avatars']['hash'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Identifier string'),
    '#description' => $this
      ->t('Generate avatar hashes for @entity_type_plural based on this string.', [
      '@entity_type_plural' => $this
        ->entityTypeManager()
        ->getDefinition($target_entity_type_id)
        ->getPluralLabel(),
    ]),
    '#default_value' => $third_party['hash']['contents'] ?? NULL,
  ];
  if ($this
    ->moduleHandler()
    ->moduleExists('token')) {

    // Add the token tree UI.
    $form['avatars']['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $target_entity_type_id,
      ],
      '#show_nested' => FALSE,
      '#global_types' => FALSE,
    ];
  }
  $form['#attached']['library'][] = 'avatars/avatars.admin';
  $form['avatars']['services'] = $this
    ->buildServiceTable($field_config);

  // Our submission function needs to be before
  // \Drupal\field_ui\Form\FieldConfigEditForm::save.
  array_unshift($form['actions']['submit']['#submit'], [
    $this,
    'fieldConfigEditFormSubmit',
  ]);
}