You are here

class DsLayout in Display Suite 8.4

Same name and namespace in other branches
  1. 8.2 src/Plugin/DsLayout.php \Drupal\ds\Plugin\DsLayout
  2. 8.3 src/Plugin/DsLayout.php \Drupal\ds\Plugin\DsLayout

Layout class for all Display Suite layouts.

Hierarchy

Expanded class hierarchy of DsLayout

1 file declares its use of DsLayout
ds.module in ./ds.module
Display Suite core functions.
3 string references to 'DsLayout'
ds.layouts.yml in ./ds.layouts.yml
ds.layouts.yml
ds_test.layouts.yml in tests/modules/ds_test/ds_test.layouts.yml
tests/modules/ds_test/ds_test.layouts.yml
ds_test_layout_theme.layouts.yml in tests/themes/ds_test_layout_theme/ds_test_layout_theme.layouts.yml
tests/themes/ds_test_layout_theme/ds_test_layout_theme.layouts.yml

File

src/Plugin/DsLayout.php, line 18

Namespace

Drupal\ds\Plugin
View source
class DsLayout extends LayoutDefault implements PluginFormInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'wrappers' => [],
      'outer_wrapper' => 'div',
      'attributes' => '',
      'link_attribute' => '',
      'link_custom' => '',
      'classes' => [
        'layout_class' => [],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['wrappers'] = $form_state
      ->getValue('region_wrapper');
    foreach ([
      'outer_wrapper',
      'attributes',
      'link_attribute',
      'link_custom',
    ] as $name) {
      $this->configuration[$name] = $this->configuration['wrappers'][$name];
      unset($this->configuration['wrappers'][$name]);
    }

    // Apply Xss::filter to attributes.
    $this->configuration['attributes'] = Xss::filter($this->configuration['attributes']);

    // In case classes is missing entirely, use the defaults.
    $defaults = $this
      ->defaultConfiguration();
    $this->configuration['classes'] = $form_state
      ->getValue('ds_classes', $defaults['classes']);

    // Do not save empty classes.
    foreach ($this->configuration['classes'] as $region_name => &$classes) {
      foreach ($classes as $class) {
        if (empty($class)) {
          unset($classes[$class]);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DsLayout::buildConfigurationForm public function Form constructor. Overrides LayoutDefault::buildConfigurationForm
DsLayout::defaultConfiguration public function Gets default configuration for this plugin. Overrides LayoutDefault::defaultConfiguration
DsLayout::submitConfigurationForm public function Form submission handler. Overrides LayoutDefault::submitConfigurationForm
DsLayout::validateConfigurationForm public function Form validation handler. Overrides LayoutDefault::validateConfigurationForm
LayoutDefault::$pluginDefinition protected property The layout definition. Overrides PluginBase::$pluginDefinition
LayoutDefault::build public function Build a render array for layout with regions. Overrides LayoutInterface::build 3
LayoutDefault::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 2
LayoutDefault::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
LayoutDefault::getPluginDefinition public function Overrides PluginBase::getPluginDefinition
LayoutDefault::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
LayoutDefault::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.