You are here

public function ParagraphsSliderPlugin::buildConfigurationForm in Paragraphs Collection 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 ParagraphsBehaviorBase::buildConfigurationForm

File

modules/paragraphs_collection_demo/src/Plugin/paragraphs/Behavior/ParagraphsSliderPlugin.php, line 110

Class

ParagraphsSliderPlugin
Provides Slider plugin.

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $paragraphs_type = $form_state
    ->getFormObject()
    ->getEntity();
  if ($paragraphs_type
    ->isNew()) {
    return [];
  }
  $field_name_options = $this
    ->getFieldsByCardinalityGreaterOne($paragraphs_type);

  // Show field mapping select form only if this entity has at least
  // one mappable field.
  if ($field_name_options) {
    $form['field_name'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Slider field'),
      '#description' => $this
        ->t('Choose the field to be used as slider items.'),
      '#options' => $field_name_options,
      '#default_value' => $this->configuration['field_name'],
    ];
  }
  else {
    $form['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('There are no fields available with the cardinality greater than one. Please add at least one in the <a href=":link">Manage fields</a> page.', [
        ':link' => Url::fromRoute("entity.{$paragraphs_type->getEntityType()->getBundleOf()}.field_ui_fields", [
          $paragraphs_type
            ->getEntityTypeId() => $paragraphs_type
            ->id(),
        ])
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'messages messages--error',
        ],
      ],
    ];
  }
  $form['slick_slider'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Slick Optionsets'),
    '#description' => $this
      ->getOptionsetDescription($this
      ->getAllOptionSet()),
    '#options' => $this
      ->getAllOptionSet(),
    '#default_value' => $this->configuration['slick_slider'],
  ];
  return $form;
}