You are here

class ErlLayout in Entity Reference with Layout 8

Layout class with various formatting options for custom ERL layouts.

Hierarchy

Expanded class hierarchy of ErlLayout

1 string reference to 'ErlLayout'
erl_layouts.layouts.yml in modules/erl_layouts/erl_layouts.layouts.yml
modules/erl_layouts/erl_layouts.layouts.yml

File

modules/erl_layouts/src/Plugin/Layout/ErlLayout.php, line 13

Namespace

Drupal\erl_layouts\Plugin\Layout
View source
class ErlLayout extends LayoutDefault implements PluginFormInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();
    $configuration += [
      'layout_classes' => '',
      'layout_bg_color' => '',
    ];
    foreach ($this
      ->getPluginDefinition()
      ->getRegions() as $region => $region_info) {
      $configuration[$region]['classes'] = '';
      $configuration[$region]['bg_color'] = '';
    }
    return $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $regions) {
    $configuration = $this
      ->getConfiguration();
    $build = parent::build($regions);
    if (!empty($configuration['layout_classes'])) {
      $layout_classes = is_array($configuration['layout_classes']) ? implode(' ', $configuration['layout_classes']) : $configuration['layout_classes'];
      $build['#attributes']['class'][] = $layout_classes;
    }
    if (!empty($configuration['layout_bg_color'])) {
      $build['#attributes']['style'] = 'background-color: ' . $configuration['layout_bg_color'];
    }
    foreach ($this
      ->getPluginDefinition()
      ->getRegionNames() as $region_name) {
      if (array_key_exists($region_name, $regions)) {
        if ($configuration[$region_name]['classes']) {
          $region_classes = is_array($configuration[$region_name]['classes']) ? implode(' ', $configuration[$region_name]['classes']) : $configuration[$region_name]['classes'];
          $build[$region_name]['#attributes']['class'][] = $region_classes;
        }
        if ($configuration[$region_name]['bg_color']) {
          $build[$region_name]['#attributes']['style'] = 'background-color: ' . $configuration[$region_name]['bg_color'];
        }
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   *
   * Add a process callback so $form[#parents] are correctly populated.
   *
   * If we were adding options that did not depend on
   * third party widget settings, using #process would be unnecessary.
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['#process'] = [
      [
        $this,
        'processConfigurationForm',
      ],
    ];
    return $form;
  }

  /**
   * Add the options.
   *
   * @param array $form_item
   *   The Form Item.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The Current state of the form.
   */
  public function processConfigurationForm(array $form_item, FormStateInterface $form_state) {
    $form_object = $form_state
      ->getFormObject();
    if (!$form_object instanceof ContentEntityFormInterface) {
      return NULL;
    }
    $display = $form_object
      ->getFormDisplay($form_state);
    $component = $display
      ->getComponent($form_item['#parents'][0]);

    // @todo There must be a better way to set third_party_settings defaults!
    $settings = [
      'layout_class_mode' => 'manual',
      'layout_color_mode' => 'manual',
      'region_class_mode' => 'manual',
      'region_color_mode' => 'manual',
    ];
    if (isset($component['third_party_settings']['erl_layouts'])) {
      $settings = $component['third_party_settings']['erl_layouts'];
    }
    $config = $this
      ->getConfiguration();
    switch ($settings['layout_class_mode']) {
      case 'manual':
        $form_item['layout_classes'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Classes'),
          '#default_value' => !empty($config['layout_classes']) ? $config['layout_classes'] : '',
          '#description' => $this
            ->t('Add classes to this layout. The classes must be separated by a space.'),
          '#size' => '100',
        ];
        break;
      case 'select':
        $classes_available = $this
          ->getSelectOptions($settings['layout_class_select']);
        $default_value = !empty($config['layout_classes']) ? $config['layout_classes'] : '';
        $form_item['layout_classes'] = [
          '#type' => 'select',
          '#multiple' => TRUE,
          '#title' => $this
            ->t('Classes'),
          '#options' => $classes_available,
          '#default_value' => $default_value,
        ];
        break;
      case 'force':
        $form_item['layout']['layout_classes'] = [
          '#type' => 'value',
          '#value' => $settings['layout_class_force'],
        ];
        break;
    }
    switch ($settings['layout_color_mode']) {
      case 'manual':
        $form_item['layout_bg_color'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Background color'),
          '#default_value' => !empty($config['layout_bg_color']) ? $config['layout_bg_color'] : '',
          '#description' => $this
            ->t('Background color (hex code) for this layout.'),
          '#attributes' => [
            'placeholder' => '#ffffff',
          ],
          '#size' => '12',
        ];
        break;
      case 'select':
        $colors_available = $this
          ->getSelectOptions($settings['layout_color_select']);
        $default_value = !empty($config['layout_bg_color']) ? $config['layout_bg_color'] : '';
        $form_item['layout_bg_color'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Background color'),
          '#options' => $colors_available,
          '#default_value' => $default_value,
        ];
        break;
      case 'force':
        $form_item['layout_bg_color'] = [
          '#type' => 'value',
          '#value' => $settings['layout_color_force'],
        ];
        break;
    }
    foreach ($this
      ->getPluginDefinition()
      ->getRegions() as $region => $region_info) {
      $region_label = $region_info['label'];
      $form_item[$region] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('@region region', [
          '@region' => $region_label,
        ]),
      ];
      $visible_items = FALSE;
      switch ($settings['region_class_mode']) {
        case 'manual':
          $form_item[$region]['classes'] = [
            '#type' => 'textfield',
            '#title' => $this
              ->t('Classes'),
            '#default_value' => !empty($config[$region]['classes']) ? $config[$region]['classes'] : '',
            '#description' => $this
              ->t('Add classes to this region. The classes must be separated by a space.'),
            '#size' => '100',
          ];
          $visible_items = TRUE;
          break;
        case 'select':
          $classes_available = $this
            ->getSelectOptions($settings['region_class_select']);
          $default_value = !empty($config[$region]['classes']) ? $config[$region]['classes'] : [];
          $form_item[$region]['classes'] = [
            '#type' => 'select',
            '#multiple' => TRUE,
            '#title' => $this
              ->t('Classes'),
            '#options' => $classes_available,
            '#default_value' => $default_value,
          ];
          $visible_items = TRUE;
          break;
        case 'force':
          $form_item[$region]['classes'] = [
            '#type' => 'value',
            '#value' => $settings['region_class_force'],
          ];
          break;
      }
      switch ($settings['region_color_mode']) {
        case 'manual':
          $form_item[$region]['bg_color'] = [
            '#type' => 'textfield',
            '#title' => $this
              ->t('@region_label region background color', [
              '@region_label' => $region_label,
            ]),
            '#default_value' => !empty($config[$region]['bg_color']) ? $config[$region]['bg_color'] : '',
            '#description' => $this
              ->t('Background color (hex code) for this region.'),
            '#attributes' => [
              'placeholder' => '#ffffff',
            ],
            '#size' => '12',
          ];
          $visible_items = TRUE;
          break;
        case 'select':
          $colors_available = $this
            ->getSelectOptions($settings['region_color_select']);
          $default_value = !empty($config[$region]['bg_color']) ? $config[$region]['bg_color'] : '';
          $form_item[$region]['bg_color'] = [
            '#type' => 'select',
            '#title' => $this
              ->t('Background color'),
            '#options' => [
              '' => $this
                ->t('- None -'),
            ] + $colors_available,
            '#default_value' => $default_value,
          ];
          $visible_items = TRUE;
          break;
        case 'force':
          $form_item[$region]['bg_color'] = [
            '#type' => 'value',
            '#value' => $settings['region_color_force'],
          ];
          break;
      }
      if (!$visible_items) {
        $form_item[$region]['#access'] = FALSE;
      }
    }
    return $form_item;
  }

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

    // Any additional form validation that is required.
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['layout_classes'] = $form_state
      ->getValue('layout_classes');
    $this->configuration['layout_bg_color'] = $form_state
      ->getValue('layout_bg_color');
    $this->configuration['column_priority'] = $form_state
      ->getValue('column_priority');
    foreach ($this
      ->getPluginDefinition()
      ->getRegions() as $region => $region_info) {
      $this->configuration[$region]['classes'] = $form_state
        ->getValue([
        $region,
        'classes',
      ]);
      $this->configuration[$region]['bg_color'] = $form_state
        ->getValue([
        $region,
        'bg_color',
      ]);
    }
  }

  /**
   * Convert textarea lines into an array.
   *
   * @param string $string
   *   The textarea lines to explode.
   * @param bool $summary
   *   A flag to return a formatted list of classes available.
   *
   * @return array
   *   An array keyed by the classes.
   */
  protected function getSelectOptions($string, $summary = FALSE) {
    $options = [];
    $lines = preg_split("/\\r\\n|\\r|\\n/", trim($string));
    $lines = array_filter($lines);
    foreach ($lines as $line) {
      list($class, $label) = explode('|', trim($line));
      $label = $label ?: $class;
      $options[$class] = $label;
    }
    if ($summary) {
      return implode(', ', array_keys($options));
    }
    return $options;
  }

}

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
ErlLayout::build public function Build a render array for layout with regions. Overrides LayoutDefault::build
ErlLayout::buildConfigurationForm public function Add a process callback so $form[#parents] are correctly populated. Overrides LayoutDefault::buildConfigurationForm
ErlLayout::defaultConfiguration public function Gets default configuration for this plugin. Overrides LayoutDefault::defaultConfiguration
ErlLayout::getSelectOptions protected function Convert textarea lines into an array.
ErlLayout::processConfigurationForm public function Add the options.
ErlLayout::submitConfigurationForm public function Form submission handler. Overrides LayoutDefault::submitConfigurationForm
ErlLayout::validateConfigurationForm public function Form validation handler. Overrides LayoutDefault::validateConfigurationForm
LayoutDefault::$pluginDefinition protected property The layout definition. Overrides PluginBase::$pluginDefinition
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.