You are here

public function AsyncMetatagFirehose::formElement in Metatag Asynchronous Widget 1.0.x

File

src/Plugin/Field/FieldWidget/AsyncMetatagFirehose.php, line 68

Class

AsyncMetatagFirehose
Asynchronous widget for the Metatag field.

Namespace

Drupal\metatag_async_widget\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('metatag_async_widget_customize_meta_tags')) {
    $element += parent::formElement($items, $delta, $element, $form, $form_state);

    // Open the meta tags group upon selection.
    $element['#open'] = TRUE;

    // Make sure that basic details is opened by default and all the others
    // are closed.
    foreach (Element::children($element) as $key) {
      if (isset($element[$key]['#type']) && $element[$key]['#type'] == 'details') {
        $element[$key]['#open'] = $key == 'basic';
      }
    }
  }
  else {
    $wrapper_id = Html::getUniqueId('metatag-async-widget-wrapper');
    $element['metatag_async_widget_customize_meta_tags'] = [
      '#type' => 'submit',
      '#name' => 'metatag_async_widget_customize_meta_tags',
      '#value' => $this
        ->t('Customize meta tags'),
      '#limit_validation_errors' => [],
      '#submit' => [
        [
          get_called_class(),
          'customizeMetaTagsSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          __CLASS__,
          'ajaxFormRefresh',
        ],
        'wrapper' => $wrapper_id,
      ],
      '#prefix' => "<span id=\"{$wrapper_id}\">",
      '#suffix' => "</span>",
    ];

    // Put the form element into the form's "advanced" group.
    // Add the outer fieldset.
    $element += [
      '#type' => 'details',
    ];

    // If the "sidebar" option was checked on the field widget, put the
    // form element into the form's "advanced" group. Otherwise, let it
    // default to the main field area.
    $sidebar = $this
      ->getSetting('sidebar');
    if ($sidebar) {
      $element['#group'] = 'advanced';
    }
  }
  return $element;
}