You are here

public function DsLayout::buildConfigurationForm in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/DsLayout.php \Drupal\ds\Plugin\DsLayout::buildConfigurationForm()
  2. 8.2 src/Plugin/DsLayout.php \Drupal\ds\Plugin\DsLayout::buildConfigurationForm()

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 LayoutDefault::buildConfigurationForm

File

src/Plugin/DsLayout.php, line 39

Class

DsLayout
Layout class for all Display Suite layouts.

Namespace

Drupal\ds\Plugin

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $configuration = $this
    ->getConfiguration();
  $regions = $this
    ->getPluginDefinition()
    ->getRegions();

  // Add wrappers.
  $wrapper_options = [
    'div' => 'Div',
    'span' => 'Span',
    'section' => 'Section',
    'article' => 'Article',
    'header' => 'Header',
    'footer' => 'Footer',
    'aside' => 'Aside',
    'figure' => 'Figure',
  ];
  $form['region_wrapper'] = [
    '#group' => 'additional_settings',
    '#type' => 'details',
    '#title' => $this
      ->t('Custom wrappers'),
    '#description' => $this
      ->t('Choose a wrapper. All Display Suite layouts support this option.'),
    '#tree' => TRUE,
  ];
  foreach ($regions as $region_name => $region_definition) {
    $form['region_wrapper'][$region_name] = [
      '#type' => 'select',
      '#options' => $wrapper_options,
      '#title' => $this
        ->t('Wrapper for @region', [
        '@region' => $region_definition['label'],
      ]),
      '#default_value' => !empty($configuration['wrappers'][$region_name]) ? $configuration['wrappers'][$region_name] : 'div',
    ];
  }
  $form['region_wrapper']['outer_wrapper'] = [
    '#type' => 'select',
    '#options' => $wrapper_options,
    '#title' => $this
      ->t('Outer wrapper'),
    '#default_value' => $configuration['outer_wrapper'],
    '#weight' => 10,
  ];
  $form['region_wrapper']['attributes'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Layout attributes'),
    '#description' => 'E.g. role|navigation,data-something|some value',
    '#default_value' => $configuration['attributes'],
    '#weight' => 11,
  ];
  $form['region_wrapper']['link_attribute'] = [
    '#type' => 'select',
    '#options' => [
      '' => $this
        ->t('No link'),
      'content' => $this
        ->t('Link to content'),
      'custom' => $this
        ->t('Custom'),
      'tokens' => $this
        ->t('Tokens'),
    ],
    '#title' => $this
      ->t('Add link'),
    '#description' => $this
      ->t('This will add an onclick attribute on the layout wrapper.'),
    '#default_value' => $configuration['link_attribute'],
    '#weight' => 12,
  ];
  $form['region_wrapper']['link_custom'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom link'),
    '#description' => $this
      ->t('You may use tokens for this link if you selected tokens.'),
    '#default_value' => $configuration['link_custom'],
    '#weight' => 13,
    '#states' => [
      'visible' => [
        [
          ':input[name="layout_configuration[region_wrapper][link_attribute]"]' => [
            [
              "value" => "tokens",
            ],
            [
              "value" => "custom",
            ],
          ],
        ],
      ],
    ],
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $form['region_wrapper']['tokens'] = [
      '#title' => $this
        ->t('Tokens'),
      '#type' => 'container',
      '#weight' => 14,
      '#states' => [
        'visible' => [
          ':input[name="layout_configuration[region_wrapper][link_attribute]"]' => [
            "value" => "tokens",
          ],
        ],
      ],
    ];
    $token_types = 'all';

    // The entity is not always available.
    // See https://www.drupal.org/project/ds/issues/3137198.
    if (($form_object = $form_state
      ->getFormObject()) && $form_object instanceof EntityFormInterface && ($entity = $form_object
      ->getEntity()) && $entity instanceof EntityDisplayInterface) {
      $token_types = [
        $entity
          ->getTargetEntityTypeId(),
      ];
    }
    $form['region_wrapper']['tokens']['help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => $token_types,
      '#global_types' => TRUE,
      '#dialog' => TRUE,
    ];
  }

  // Add extra classes for the regions to have more control while theming.
  $form['ds_classes'] = [
    '#group' => 'additional_settings',
    '#type' => 'details',
    '#title' => $this
      ->t('Custom classes'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $classes_access = \Drupal::currentUser()
    ->hasPermission('admin_classes');
  $classes = Ds::getClasses();
  if (!empty($classes)) {
    $layoutSettings = $this
      ->getPluginDefinition()
      ->get('settings') ?: [];
    $default_layout_classes = isset($layoutSettings['classes']['layout_class']) ? $layoutSettings['classes']['layout_class'] : [];
    $form['ds_classes']['layout_class'] = [
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => $classes,
      '#title' => $this
        ->t('Class for layout'),
      '#default_value' => !empty($configuration['classes']['layout_class']) ? $configuration['classes']['layout_class'] : $default_layout_classes,
    ];
    foreach ($regions as $region_name => $region_definition) {
      $default_classes = isset($layoutSettings['classes'][$region_name]) ? $layoutSettings['classes'][$region_name] : [];
      $form['ds_classes'][$region_name] = [
        '#type' => 'select',
        '#multiple' => TRUE,
        '#options' => $classes,
        '#title' => $this
          ->t('Class for @region', [
          '@region' => $region_definition['label'],
        ]),
        '#default_value' => isset($configuration['classes'][$region_name]) ? $configuration['classes'][$region_name] : $default_classes,
      ];
    }
    if ($classes_access) {
      $url = Url::fromRoute('ds.classes');
      $destination = \Drupal::destination()
        ->getAsArray();
      $url
        ->setOption('query', $destination);
      $form['ds_classes']['info'] = [
        '#markup' => Link::fromTextAndUrl(t('Manage region and field CSS classes'), $url)
          ->toString(),
      ];
    }
  }
  else {
    if ($classes_access) {
      $url = Url::fromRoute('ds.classes');
      $destination = \Drupal::destination()
        ->getAsArray();
      $url
        ->setOption('query', $destination);
      $form['ds_classes']['info'] = [
        '#markup' => '<p>' . $this
          ->t('You have not defined any CSS classes which can be used on regions.') . '</p><p>' . Link::fromTextAndUrl(t('Manage region and field CSS classes'), $url)
          ->toString() . '</p>',
      ];
    }
    else {
      $form['ds_classes']['#access'] = FALSE;
    }
  }
  return $form;
}