View source
<?php
namespace Drupal\conditional_fields;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
class Conditions {
use StringTranslationTrait;
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public function conditionalFieldsDependencyDefaultSettings() {
return [
'state' => 'visible',
'condition' => 'value',
'grouping' => 'AND',
'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
'value' => '',
'values' => [],
'value_form' => [],
'effect' => 'show',
'effect_options' => [],
'selector' => '',
];
}
public function conditionalFieldsStates() {
$states = [
'visible' => $this
->t('Visible'),
'!visible' => $this
->t('Invisible'),
'!empty' => $this
->t('Filled with a value'),
'empty' => $this
->t('Emptied'),
'!disabled' => $this
->t('Enabled'),
'disabled' => $this
->t('Disabled'),
'checked' => $this
->t('Checked'),
'!checked' => $this
->t('Unchecked'),
'required' => $this
->t('Required'),
'!required' => $this
->t('Optional'),
'!collapsed' => $this
->t('Expanded'),
'collapsed' => $this
->t('Collapsed'),
'unchanged' => $this
->t('Unchanged (no state)'),
];
$this->moduleHandler
->alter('conditionalFieldsStates', $states);
return $states;
}
public function conditionalFieldsEffects() {
$effects = [
'show' => [
'label' => $this
->t('Show/Hide'),
'states' => [
'visible',
'!visible',
],
],
'fade' => [
'label' => $this
->t('Fade in/Fade out'),
'states' => [
'visible',
'!visible',
],
'options' => [
'speed' => [
'#type' => 'textfield',
'#description' => $this
->t('The speed at which the animation is performed, in milliseconds.'),
'#default_value' => 400,
],
],
],
'slide' => [
'label' => $this
->t('Slide down/Slide up'),
'states' => [
'visible',
'!visible',
],
'options' => [
'speed' => [
'#type' => 'textfield',
'#description' => $this
->t('The speed at which the animation is performed, in milliseconds.'),
'#default_value' => 400,
],
],
],
'fill' => [
'label' => $this
->t('Fill field with a value'),
'states' => [
'!empty',
],
'options' => [
'value' => [
'#type' => 'textfield',
'#description' => $this
->t('The value that should be given to the field when automatically filled.'),
'#default_value' => '',
],
'reset' => [
'#type' => 'checkbox',
'#title' => $this
->t('Restore previous value when untriggered'),
'#default_value' => 1,
],
],
],
'empty' => [
'label' => $this
->t('Empty field'),
'states' => [
'empty',
],
'options' => [
'value' => [
'#type' => 'hidden',
'#description' => $this
->t('The value that should be given to the field when automatically emptied.'),
'#value' => '',
'#default_value' => '',
],
'reset' => [
'#type' => 'checkbox',
'#title' => $this
->t('Restore previous value when untriggered'),
'#default_value' => 1,
],
],
],
];
$this->moduleHandler
->alter('conditionalFieldsEffects', $effects);
return $effects;
}
public function conditionalFieldsConditions($checkboxes = TRUE) {
$conditions = [
'!empty' => $this
->t('Filled'),
'empty' => $this
->t('Empty'),
'touched' => $this
->t('Touched'),
'!touched' => $this
->t('Untouched'),
'focused' => $this
->t('Focused'),
'!focused' => $this
->t('Unfocused'),
];
if ($checkboxes) {
$conditions['checked'] = $this
->t('Checked');
$conditions['!checked'] = $this
->t('Unchecked');
}
$conditions['value'] = $this
->t('Value');
$this->moduleHandler
->alter('conditionalFieldsConditions', $conditions);
return $conditions;
}
}