abstract class FieldStateBase in Field States UI 8
Same name and namespace in other branches
- 8.2 src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase
Provides a base class for field staes.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\field_states_ui\FieldStateBase implements ContainerFactoryPluginInterface, FieldStateInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FieldStateBase
See also
\Drupal\field_states_ui\Annotation\FieldState
\Drupal\field_states_ui\FieldStateInterface
\Drupal\field_states_ui\FieldStateManager
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
File
- src/
FieldStateBase.php, line 18
Namespace
Drupal\field_states_uiView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldStateBase:: |
protected | property | A logger instance. | |
FieldStateBase:: |
protected | property | The field state ID. | |
FieldStateBase:: |
public | function |
Applies a field state to the field widget's form element. Overrides FieldStateInterface:: |
|
FieldStateBase:: |
public | function | ||
FieldStateBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
FieldStateBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FieldStateBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurablePluginInterface:: |
|
FieldStateBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
FieldStateBase:: |
public | function | ||
FieldStateBase:: |
public | function |
Returns a render array summarizing the configuration of the image effect. Overrides FieldStateInterface:: |
|
FieldStateBase:: |
public | function |
Returns the unique ID representing the field state. Overrides FieldStateInterface:: |
|
FieldStateBase:: |
public | function |
Returns the field state label. Overrides FieldStateInterface:: |
|
FieldStateBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
FieldStateBase:: |
public | function | ||
FieldStateBase:: |
public | function | ||
FieldStateBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |