You are here

public function Animal::buildConfigurationForm in 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

tests/embed_test/src/Plugin/EmbedType/Animal.php, line 21

Class

Animal
Animal test embed type.

Namespace

Drupal\embed_test\Plugin\EmbedType

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form_object = $form_state
    ->getFormObject();
  $form['animal_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Animal type'),
    '#options' => [
      'vertebrates' => $this
        ->t('Vertebrates (with backbone)'),
      'invertebrates' => $this
        ->t('Invertebrates (without backbone)'),
    ],
    '#default_value' => $this
      ->getConfigurationValue('animal_type'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $form_object,
        'updateTypeSettings',
      ],
      'effect' => 'fade',
    ],
  ];
  if ($this
    ->getConfigurationValue('animal_type') === 'vertebrates') {
    $form['allowed_vertebrates'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Limit allowed vertebrates'),
      '#options' => [
        'amphibians' => $this
          ->t('Amphibians'),
        'birds' => $this
          ->t('Birds'),
        'fish' => $this
          ->t('Fish'),
        'mammals' => $this
          ->t('Mammals'),
        'reptiles' => $this
          ->t('Reptiles'),
      ],
      '#default_value' => $this
        ->getConfigurationValue('allowed_vertebrates', []),
    ];
  }
  return $form;
}