You are here

public function Conditions::conditionalFieldsEffects in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Conditions.php \Drupal\conditional_fields\Conditions::conditionalFieldsEffects()

Builds a list of supported effects.

That may be applied to a dependent field when it changes from visible to invisible and viceversa. The effects may have options that will be passed as Javascript settings and used by conditional_fields.js.

Return value

array An associative array of effects. Each key is an unique name for the effect. The value is an associative array:

  • label: The human readable name of the effect.
  • states: The states that can be associated with this effect.
  • options: An associative array of effect options names, field types, descriptions and default values.

File

src/Conditions.php, line 109

Class

Conditions
Provide conditional field's lists.

Namespace

Drupal\conditional_fields

Code

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,
        ],
      ],
    ],
  ];

  // Allow other modules to modify the effects.
  $this->moduleHandler
    ->alter('conditionalFieldsEffects', $effects);
  return $effects;
}