Same name in other branches
  1. 8.x-2.x 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

Title Sort descending Modifiers Object type Summary Overriden Title
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
FieldStateBase::create public static function
FieldStateBase::defaultConfiguration public function
FieldStateBase::getConfiguration public function
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
FieldStateBase::submitConfigurationForm public function
FieldStateBase::validateConfigurationForm public function
FieldStateBase::__construct public function