You are here

public function MenuLinkContentVisibilityWidget::formElement in Custom Menu Links Visibility 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldWidget/MenuLinkContentVisibilityWidget.php \Drupal\menu_link_content_visibility\Plugin\Field\FieldWidget\MenuLinkContentVisibilityWidget::formElement()

@inheritDoc

Overrides WidgetInterface::formElement

File

src/Plugin/Field/FieldWidget/MenuLinkContentVisibilityWidget.php, line 59

Class

MenuLinkContentVisibilityWidget
Plugin annotation @FieldWidget( label = @Translation("Menu link visibility"), id = "menu_link_content_visibility", field_types = { "menu_link_content_visibility" }, )

Namespace

Drupal\menu_link_content_visibility\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $value = unserialize($items[$delta]->value);
  $element['visibility_tabs'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Visibility'),
    '#parents' => [
      'visibility_tabs',
    ],
  ];
  $contexts = $this->context_repository
    ->getAvailableContexts();
  $form_state
    ->setTemporaryValue('gathered_contexts', $contexts);
  foreach ($this->condition_manager
    ->getDefinitionsForContexts($contexts) as $condition_id => $definition) {
    if ($condition_id === 'current_theme') {
      continue;
    }

    /** @var ConditionInterface $condition */
    $condition = $this->condition_manager
      ->createInstance($condition_id);
    $condition_configuration = isset($value[$condition_id]) ? $value[$condition_id] : $condition
      ->defaultConfiguration();
    $condition
      ->setConfiguration($condition_configuration);
    $condition_form = $condition
      ->buildConfigurationForm([], $form_state);
    $condition_form['#type'] = 'details';
    $condition_form['#title'] = $condition
      ->getPluginDefinition()['label'];
    $condition_form['#group'] = 'visibility_tabs';
    $element[$condition_id] = $condition_form;
  }
  return $element;
}