You are here

class Categories in Block field 8

Provides a 'categories' BlockFieldSection.

Plugin annotation


@BlockFieldSelection(
  id = "categories",
  label = @Translation("Categories"),
)

Hierarchy

Expanded class hierarchy of Categories

2 string references to 'Categories'
block_field.yml in config/schema/block_field.yml
config/schema/block_field.yml
Categories::buildConfigurationForm in src/Plugin/block_field/BlockFieldSelection/Categories.php
Form constructor.

File

src/Plugin/block_field/BlockFieldSelection/Categories.php, line 18

Namespace

Drupal\block_field\Plugin\block_field\BlockFieldSelection
View source
class Categories extends BlockFieldSelectionBase {
  use DependencySerializationTrait;
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'categories' => [],
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    /** @var \Drupal\block_field\BlockFieldManagerInterface $block_field_manager */
    $block_field_manager = \Drupal::service('block_field.manager');
    $categories = $block_field_manager
      ->getBlockCategories();
    $options = [];
    foreach ($categories as $category) {
      $category = (string) $category;
      $options[$category] = $category;
    }
    $form['categories'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Categories'),
      '#description' => $this
        ->t('Please select available categories.'),
      '#open' => empty($this
        ->getConfiguration()['categories']),
      '#process' => [
        [
          $this,
          'formProcessMergeParent',
        ],
      ],
    ];
    $default_value = !empty($this
      ->getConfiguration()['categories']) ? $this
      ->getConfiguration()['categories'] : array_keys($options);
    $form['categories']['categories'] = [
      '#type' => 'checkboxes',
      '#header' => [
        'Category',
      ],
      '#options' => $options,
      '#js_select' => TRUE,
      '#empty' => $this
        ->t('No categories are available.'),
      '#required' => TRUE,
      '#default_value' => array_combine($default_value, $default_value),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getReferenceableBlockDefinitions() {
    $block_field_manager = \Drupal::service('block_field.manager');
    $definitions = $block_field_manager
      ->getBlockDefinitions();
    if (!empty($this
      ->getConfiguration()['categories'])) {
      $categories = array_filter($this
        ->getConfiguration()['categories']);
      $definitions = array_filter($definitions, function ($definition, $key) use ($categories) {
        return isset($categories[(string) $definition['category']]);
      }, ARRAY_FILTER_USE_BOTH);
    }
    return $definitions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockFieldSelectionBase::formProcessMergeParent public function Move settings up a level for easier processing and storage.
BlockFieldSelectionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
BlockFieldSelectionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
BlockFieldSelectionBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
BlockFieldSelectionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
BlockFieldSelectionBase::__construct public function Constructs a new selection object. Overrides PluginBase::__construct
Categories::buildConfigurationForm public function Form constructor. Overrides BlockFieldSelectionBase::buildConfigurationForm
Categories::defaultConfiguration public function Gets default configuration for this plugin. Overrides BlockFieldSelectionBase::defaultConfiguration
Categories::getReferenceableBlockDefinitions public function Returns filtered block definitions based on plugin settings. Overrides BlockFieldSelectionInterface::getReferenceableBlockDefinitions
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
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 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::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
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.