pattern_library-8.x-2.x-dev/src/Plugin/PatternModifierType/Select.php

src/Plugin/PatternModifierType/Select.php
<?php

namespace Drupal\pattern_library\Plugin\PatternModifierType;

use Drupal\Component\Utility\Unicode;
use Drupal\pattern_library\Annotation\PatternModifierType;
use Drupal\pattern_library\Plugin\PatternModifierTypeBase;

/**
 * Define the pattern select modifier type.
 *
 * @PatternModifierType(
 *   id = "select"
 * )
 */
class Select extends PatternModifierTypeBase {

  /**
   * {@inheritdoc}
   */
  public function render() {
    return [
      '#type' => 'select',
      '#options' => $this->options(),
      '#multiple' => $this->isMultiple(),
      '#empty_option' => $this->t('- Default -'),
      '#default_value' => $this->defaultValue(),
    ] + parent::render();
  }

  /**
   * Define the modifier options.
   *
   * @return array
   *   An array of options.
   */
  protected function options() {
    $options = [];

    foreach ($this->getOptions() as $key => $value) {
      if (is_numeric($key)) {
        $key = mb_strtolower($value);
      }
      $options[$key] = Unicode::ucwords($value);
    }

    return $options;
  }

  /**
   * Is the modifier configuration multiple.
   *
   * @return bool
   *   Return TRUE if the configuration allows multiple options; otherwise FALSE.
   */
  protected function isMultiple() {
    return (boolean) $this->hasConfiguration('multiple')
      ? $this->getConfiguration()['multiple']
      : FALSE;
  }

  /**
   * Get the modifier configuration options.
   *
   * @return array
   *   An array of the options defined in the configurations.
   */
  protected function getOptions() {
    return $this->hasConfiguration('options')
      ? $this->getConfiguration()['options']
      : [];
  }

  /**
   * {@inheritdoc}
   */
  protected function defaultConfiguration() {
    return [
      'description' => 'Select the value to use for the modifier.'
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc