You are here

public function Entity::buildConfigurationForm in Entity Embed 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides EmbedTypeBase::buildConfigurationForm

File

src/Plugin/EmbedType/Entity.php, line 115

Class

Entity
Entity embed type.

Namespace

Drupal\entity_embed\Plugin\EmbedType

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $embed_button = $form_state
    ->getTemporaryValue('embed_button');
  $entity_type_id = $this
    ->getConfigurationValue('entity_type');
  $form['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#options' => $this
      ->getEntityTypeOptions(),
    '#default_value' => $entity_type_id,
    '#description' => $this
      ->t("The entity type this button will embed."),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $form_state
          ->getFormObject(),
        'updateTypeSettings',
      ],
      'effect' => 'fade',
    ],
    '#disabled' => !$embed_button
      ->isNew(),
  ];
  if ($entity_type_id) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $form['bundles'] = [
      '#type' => 'checkboxes',
      '#title' => $entity_type
        ->getBundleLabel() ?: $this
        ->t('Bundles'),
      '#options' => $this
        ->getEntityBundleOptions($entity_type),
      '#default_value' => $this
        ->getConfigurationValue('bundles'),
      '#description' => $this
        ->t('If none are selected, all are allowed.'),
    ];
    $form['bundles']['#access'] = !empty($form['bundles']['#options']);

    // Allow option to limit Entity Embed Display plugins.
    $form['display_plugins'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Allowed Entity Embed Display plugins'),
      '#options' => $this->displayPluginManager
        ->getDefinitionOptionsForEntityType($entity_type_id),
      '#default_value' => $this
        ->getConfigurationValue('display_plugins'),
      '#description' => $this
        ->t('If none are selected, all are allowed. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.'),
    ];
    $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']);

    /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */
    if ($this->entityTypeManager
      ->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager
      ->getStorage('entity_browser')
      ->loadMultiple())) {

      // Filter out unsupported displays & return array of ids and labels.
      $browsers = array_map(function ($item) {

        /** @var \Drupal\entity_browser\EntityBrowserInterface $item */
        return $item
          ->label();
      }, array_filter($browsers, function (EntityBrowserInterface $browser) {
        return !in_array($browser
          ->getDisplay()
          ->getPluginId(), [
          'modal',
          'standalone',
        ], TRUE);
      }));
      $options = [
        '_none' => $this
          ->t('None (autocomplete)'),
      ] + $browsers;
      $form['entity_browser'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Entity browser'),
        '#description' => $this
          ->t('Entity browser to be used to select entities to be embedded. Only compatible browsers will be available to be chosen.'),
        '#options' => $options,
        '#default_value' => $this
          ->getConfigurationValue('entity_browser'),
      ];
      $form['entity_browser_settings'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Entity browser settings'),
        '#open' => TRUE,
        '#states' => [
          'invisible' => [
            ':input[name="type_settings[entity_browser]"]' => [
              'value' => '_none',
            ],
          ],
        ],
      ];
      $form['entity_browser_settings']['display_review'] = [
        '#type' => 'checkbox',
        '#title' => 'Display the entity after selection',
        '#default_value' => $this
          ->getConfigurationValue('entity_browser_settings')['display_review'],
      ];
    }
    else {
      $form['entity_browser'] = [
        '#type' => 'value',
        '#value' => '',
      ];
    }
  }
  return $form;
}