You are here

public function EntityBrowserEditForm::form in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 src/Form/EntityBrowserEditForm.php \Drupal\entity_browser\Form\EntityBrowserEditForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/EntityBrowserEditForm.php, line 86

Class

EntityBrowserEditForm
Class EntityBrowserEditForm.

Namespace

Drupal\entity_browser\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\entity_browser\Entity\EntityBrowser $entity_browser */
  $entity_browser = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $entity_browser
      ->label(),
    '#description' => $this
      ->t("Label for the Entity Browser."),
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'machine_name',
    '#default_value' => $entity_browser
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\entity_browser\\Entity\\EntityBrowser::load',
    ],
    '#disabled' => !$entity_browser
      ->isNew(),
  ];
  if ($entity_browser
    ->isNew()) {
    $help_text = '<div class="clearfix eb-help-text"><h2>' . $this
      ->t('Entity Browser creation instructions') . '</h2>';
    $help_text .= '<p>' . $this
      ->t('When you save this form, you will be taken to another form to configure widgets for the entity browser.') . '</p>';
    $help_text .= '<p>' . $this
      ->t('You can find more detailed information about creating and configuring Entity Browsers at the <a href="@guide_href" target="_blank">official documentation</a>.', [
      '@guide_href' => 'https://drupal-media.gitbooks.io/drupal8-guide/content/modules/entity_browser/intro.html',
    ]) . '</p>';
    $help_text .= '</div>';
    $form['help_text'] = [
      '#markup' => $help_text,
    ];
  }
  $form['display_wrapper'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Display'),
  ];
  $form['display_wrapper']['description'] = $this
    ->getPluginDescription('display');

  // Default if not set.
  if (empty($entity_browser->display)) {
    $entity_browser
      ->setDisplay('modal');
  }
  $display_plugin = $entity_browser
    ->getDisplay();
  $form['display_wrapper']['display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Display plugin'),
    '#default_value' => $display_plugin
      ->getPluginId(),
    '#options' => $this
      ->getPluginOptions('display'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'displayPluginAjaxCallback',
      ],
      'wrapper' => 'display-config-ajax-wrapper',
      'event' => 'change',
    ],
    '#executes_submit_callback' => TRUE,
    '#submit' => [
      [
        get_class($this),
        'submitUpdateDisplayPluginSettings',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'display',
      ],
    ],
  ];
  $form['display_wrapper']['display_configuration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Display Plugin settings'),
    '#open' => FALSE,
    '#prefix' => '<div id="display-config-ajax-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  if ($display_config_form = $display_plugin
    ->buildConfigurationForm([], $form_state)) {
    $form['display_wrapper']['display_configuration'] = array_merge($form['display_wrapper']['display_configuration'], $display_config_form);
  }
  else {
    $form['display_wrapper']['display_configuration']['no_options'] = [
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => $this
        ->t('This plugin has no configuration options.'),
    ];
  }
  $form['widget_selector_wrapper'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Widget Selector'),
  ];
  $form['widget_selector_wrapper']['description'] = $this
    ->getPluginDescription('widget_selector');

  // Set default if empty.
  if (empty($entity_browser->widget_selector)) {
    $entity_browser
      ->setWidgetSelector('tabs');
  }
  $widget_selector_plugin = $entity_browser
    ->getWidgetSelector();
  $form['widget_selector_wrapper']['widget_selector'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Widget selector plugin'),
    '#default_value' => $widget_selector_plugin
      ->getPluginId(),
    '#options' => $this
      ->getPluginOptions('widget_selector'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'widgetSelectorAjaxCallback',
      ],
      'wrapper' => 'widget-selector-config-ajax-wrapper',
      'event' => 'change',
    ],
    '#executes_submit_callback' => TRUE,
    '#submit' => [
      [
        get_class($this),
        'submitUpdateWidgetSelector',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'widget_selector',
      ],
    ],
  ];
  $form['widget_selector_wrapper']['widget_selector_configuration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Widget Selector Plugin settings'),
    '#open' => FALSE,
    '#prefix' => '<div id="widget-selector-config-ajax-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  if ($widget_selector_config_form = $widget_selector_plugin
    ->buildConfigurationForm([], $form_state)) {
    $form['widget_selector_wrapper']['widget_selector_configuration'] = array_merge($form['widget_selector_wrapper']['widget_selector_configuration'], $widget_selector_config_form);
  }
  else {
    $form['widget_selector_wrapper']['widget_selector_configuration']['no_options'] = [
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => $this
        ->t('This plugin has no configuration options.'),
    ];
  }
  $form['selection_display_wrapper'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Selection Display'),
  ];
  $form['selection_display_wrapper']['description'] = $this
    ->getPluginDescription('selection_display');
  if (empty($entity_browser->selection_display)) {
    $entity_browser
      ->setSelectionDisplay('no_display');
  }
  $selection_display = $entity_browser
    ->getSelectionDisplay();
  $form['selection_display_wrapper']['selection_display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Selection display plugin'),
    '#default_value' => $selection_display
      ->getPluginId(),
    '#options' => $this
      ->getPluginOptions('selection_display'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'selectionDisplayAjaxCallback',
      ],
      'wrapper' => 'selection-display-config-ajax-wrapper',
      'event' => 'change',
    ],
    '#executes_submit_callback' => TRUE,
    '#submit' => [
      [
        get_class($this),
        'submitUpdateSelectionDisplay',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'selection_display',
      ],
    ],
  ];
  $form['selection_display_wrapper']['selection_display_configuration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Selection Display Plugin settings'),
    '#open' => FALSE,
    '#prefix' => '<div id="selection-display-config-ajax-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  if ($selection_display_config_form = $selection_display
    ->buildConfigurationForm([], $form_state)) {
    $form['selection_display_wrapper']['selection_display_configuration'] = array_merge($form['selection_display_wrapper']['selection_display_configuration'], $selection_display_config_form);
  }
  else {
    $form['selection_display_wrapper']['selection_display_configuration']['no_options'] = [
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => $this
        ->t('This plugin has no configuration options.'),
    ];
  }
  return $form;
}