Same name in other branches
- 8.x-2.x src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase
Provides a base class for field staes.
Hierarchy
- class \Drupal\field_states_ui\FieldStateBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\field_states_ui\FieldStateInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
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
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;
}
}