You are here

class DefaultStateHandler in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/Plugin/conditional_fields/handler/DefaultStateHandler.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\DefaultStateHandler

Provides states handler for text fields.

Plugin annotation


@ConditionalFieldsHandler(
  id = "states_handler_default_state",
)

Hierarchy

Expanded class hierarchy of DefaultStateHandler

File

src/Plugin/conditional_fields/handler/DefaultStateHandler.php, line 15

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler
View source
class DefaultStateHandler extends ConditionalFieldsHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function statesHandler($field, $field_info, $options) {

    // Build the values that trigger the dependency.
    $values = [];
    $values_array = $this
      ->getConditionValues($options);
    $values_set = $options['values_set'];
    switch ($values_set) {
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
        $values[$options['condition']] = $options['value_form'];
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
        $values[$options['condition']] = [
          'regex' => $options['regex'],
        ];
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
        $values[$options['condition']] = [
          'xor' => $values_array,
        ];
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
        $values[$options['condition']] = count($values_array) == 1 ? $values_array[0] : $values_array;
        break;
      default:
        if ($options['values_set'] == ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT) {
          $options['state'] = '!' . $options['state'];
        }

        // OR, NOT conditions are obtained with a nested array.
        if (!empty($values_array)) {
          foreach ($values_array as $value) {
            $values[] = [
              'value' => $value,
            ];
          }
        }
        else {
          $values = $options['values'];
        }
        break;
    }
    $state = [
      $options['state'] => [
        $options['selector'] => $values,
      ],
    ];
    return $state;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionalFieldsHandlerBase::getConditionValues public function Get values form the condition options. Overrides ConditionalFieldsHandlersPluginInterface::getConditionValues
ConditionalFieldsHandlerBase::getWidgetValue public function Get values from widget settings for plugin. Overrides ConditionalFieldsHandlersPluginInterface::getWidgetValue 5
DefaultStateHandler::statesHandler public function Executes states handler according to conditional fields settings. Overrides ConditionalFieldsHandlersPluginInterface::statesHandler