You are here

class Conditions in Conditional Fields 8

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

Provide conditional field's lists.

Hierarchy

Expanded class hierarchy of Conditions

2 files declare their use of Conditions
ConditionalFieldEditForm.php in src/Form/ConditionalFieldEditForm.php
ConditionalFieldForm.php in src/Form/ConditionalFieldForm.php
2 string references to 'Conditions'
conditional_fields.schema.yml in config/schema/conditional_fields.schema.yml
config/schema/conditional_fields.schema.yml
conditional_fields.services.yml in ./conditional_fields.services.yml
conditional_fields.services.yml
1 service uses Conditions
conditional_fields.conditions in ./conditional_fields.services.yml
Drupal\conditional_fields\Conditions

File

src/Conditions.php, line 11

Namespace

Drupal\conditional_fields
View source
class Conditions {
  use StringTranslationTrait;

  /**
   * The manages modules.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The construct method.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The manages modules.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * Provides default options for a dependency.
   */
  public function conditionalFieldsDependencyDefaultSettings() {
    return [
      'state' => 'visible',
      'condition' => 'value',
      'grouping' => 'AND',
      'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
      // !Important.
      // The param default value MUST match to schema declaration.
      // @see conditional_fields.schema.yml
      'value' => '',
      'values' => [],
      'value_form' => [],
      'effect' => 'show',
      'effect_options' => [],
      'selector' => '',
    ];
  }

  /**
   * Builds a list of supported states that may be applied to a dependent field.
   */
  public function conditionalFieldsStates() {
    $states = [
      // Supported by States API.
      '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'),
      // Supported by Conditional Fields.
      'unchanged' => $this
        ->t('Unchanged (no state)'),
    ];

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

  /**
   * 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 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.
   */
  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;
  }

  /**
   * List of states of a control field that may be used to evaluate a condition.
   */
  public function conditionalFieldsConditions($checkboxes = TRUE) {

    // Supported by States API.
    $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) {

      // Relevant only if control is a list of checkboxes.
      $conditions['checked'] = $this
        ->t('Checked');
      $conditions['!checked'] = $this
        ->t('Unchecked');
    }
    $conditions['value'] = $this
      ->t('Value');

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

}

Members

Namesort descending Modifiers Type Description Overrides
Conditions::$moduleHandler protected property The manages modules.
Conditions::conditionalFieldsConditions public function List of states of a control field that may be used to evaluate a condition.
Conditions::conditionalFieldsDependencyDefaultSettings public function Provides default options for a dependency.
Conditions::conditionalFieldsEffects public function Builds a list of supported effects.
Conditions::conditionalFieldsStates public function Builds a list of supported states that may be applied to a dependent field.
Conditions::__construct public function The construct method.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.