You are here

abstract class FieldStateBase in Field States UI 8

Same name and namespace in other branches
  1. 8.2 src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase

Provides a base class for field staes.

Hierarchy

Expanded class hierarchy of FieldStateBase

See also

\Drupal\field_states_ui\Annotation\FieldState

\Drupal\field_states_ui\FieldStateInterface

\Drupal\field_states_ui\FieldStateManager

Plugin API

10 files declare their use of FieldStateBase
Checked.php in src/Plugin/FieldState/Checked.php
Collapsed.php in src/Plugin/FieldState/Collapsed.php
Disabled.php in src/Plugin/FieldState/Disabled.php
Enabled.php in src/Plugin/FieldState/Enabled.php
Expanded.php in src/Plugin/FieldState/Expanded.php

... See full list

File

src/FieldStateBase.php, line 18

Namespace

Drupal\field_states_ui
View source
abstract class FieldStateBase extends PluginBase implements FieldStateInterface, ContainerFactoryPluginInterface {

  /**
   * The field state ID.
   *
   * @var string
   */
  protected $uuid;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function applyState(array &$states, FormStateInterface $form_state, array $context, array $element, array $parents = NULL) {
    $target_field = $form_state
      ->getFormObject()
      ->getFormDisplay($form_state)
      ->getComponent($this->configuration['target']);

    // If dealing with a field on an Inline Entity Form or a Field Collection
    // have to include the field parents in the selector.
    if (!empty($parents)) {
      $target = array_shift($parents) . '[' . implode('][', $parents) . '][' . $this->configuration['target'] . ']';
    }
    else {
      $target = $this->configuration['target'];
    }
    switch ($target_field['type']) {
      case 'options_select':
        $selector = "select[name^='{$target}']";
        break;
      default:
        $selector = ":input[name^='{$target}']";
        break;
    }
    $states[$this->pluginDefinition['id']][] = [
      $selector => [
        $this->configuration['comparison'] => $this->configuration['value'],
      ],
    ];
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    return [
      '#theme' => 'field_states_ui_summary',
      '#data' => $this->configuration,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function getUuid() {
    return $this->uuid;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return [
      'uuid' => $this
        ->getUuid(),
      'id' => $this
        ->getPluginId(),
      'data' => $this->configuration,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $configuration += [
      'data' => [],
      'uuid' => '',
    ];
    $this->configuration = $configuration['data'] + $this
      ->defaultConfiguration();
    if (!$this->configuration['value']) {
      $this->configuration['value'] = TRUE;
    }
    $this->uuid = $configuration['uuid'] ? $configuration['uuid'] : \Drupal::service('uuid')
      ->generate();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigurationForForm() {
    $form = [];
    foreach ($this->configuration as $key => $value) {
      $form[$key] = [
        '#type' => 'hidden',
        '#value' => $value,
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'value' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->setConfiguration([
      'data' => $form_state
        ->getValues(),
      'uuid' => $this->uuid,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $display = $form_state
      ->getFormObject()
      ->getEntity();
    $fields = [];
    $definitions = \Drupal::entityManager()
      ->getFieldDefinitions($display
      ->getTargetEntityTypeId(), $display
      ->getTargetBundle());
    $current_field = $form_state
      ->get('field_states_ui_edit');
    foreach ($display
      ->getComponents() as $name => $field) {
      if (!isset($definitions[$name]) || $name === $current_field) {
        continue;
      }
      $fields[$name] = $definitions[$name]
        ->getLabel();
    }
    asort($fields, SORT_NATURAL | SORT_FLAG_CASE);
    $form['target'] = [
      '#type' => 'select',
      '#title' => t('Target'),
      '#description' => t('The field to run a comparison on'),
      '#required' => TRUE,
      '#other' => t('Other element on the page'),
      '#other_description' => t('Should be a valid jQuery style element selector.'),
      '#options' => $fields,
      '#default_value' => isset($this->configuration['target']) ? $this->configuration['target'] : '',
    ];
    $form['comparison'] = [
      '#type' => 'select',
      '#title' => t('Comparison Type'),
      '#options' => [
        'empty' => 'empty',
        'filled' => 'filled',
        'checked' => 'checked',
        'unchecked' => 'unchecked',
        'expanded' => 'expanded',
        'collapsed' => 'collapsed',
        'value' => 'value',
      ],
      '#default_value' => isset($this->configuration['comparison']) ? $this->configuration['comparison'] : '',
    ];
    $form['value'] = [
      '#type' => 'textfield',
      '#title' => t('Value'),
      '#default_value' => isset($this->configuration['value']) ? $this->configuration['value'] : '',
      '#states' => [
        'visible' => [
          'select[name$="[comparison]"]' => [
            'value' => 'value',
          ],
        ],
      ],
    ];
    return $form;
  }

}

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
FieldStateBase::$logger protected property A logger instance.
FieldStateBase::$uuid protected property The field state ID.
FieldStateBase::applyState public function Applies a field state to the field widget's form element. Overrides FieldStateInterface::applyState
FieldStateBase::buildConfigurationForm public function
FieldStateBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FieldStateBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldStateBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration
FieldStateBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
FieldStateBase::getConfigurationForForm public function
FieldStateBase::getSummary public function Returns a render array summarizing the configuration of the image effect. Overrides FieldStateInterface::getSummary
FieldStateBase::getUuid public function Returns the unique ID representing the field state. Overrides FieldStateInterface::getUuid
FieldStateBase::label public function Returns the field state label. Overrides FieldStateInterface::label
FieldStateBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
FieldStateBase::submitConfigurationForm public function
FieldStateBase::validateConfigurationForm public function
FieldStateBase::__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::$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.