You are here

class Checkbox in Conditional Fields 8

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

Provides states handler for single on/off checkbox.

Plugin annotation


@ConditionalFieldsHandler(
  id = "states_handler_boolean_checkbox",
)

Hierarchy

Expanded class hierarchy of Checkbox

File

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

Namespace

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

  /**
   * {@inheritdoc}
   *
   * @TODO: Different handlers for boolean and list fields.
   */
  public function statesHandler($field, $field_info, $options) {
    $state = [];
    $checked = FALSE;
    switch ($options['values_set']) {
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
        $widget_value = $this
          ->getWidgetValue($options['value_form']);
        $checked = $field['#return_value'] == $widget_value;
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
        $checked = preg_match('/' . $options['value']['RegExp'] . '/', $field['#on_value']) ? TRUE : FALSE;
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:

        // ANDing values of a single checkbox doesn't make sense:
        // just use the first value.
        $checked = $options['values'][0] == $field['#on_value'] ? TRUE : FALSE;
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
        $checked = in_array($field['#on_value'], $options['values']) ? TRUE : FALSE;
        break;
    }
    $state[$options['state']][$options['selector']] = [
      'checked' => $checked,
    ];
    return $state;
  }

  /**
   * Get values from widget settings for plugin.
   *
   * @param array $value_form
   *   Dependency options.
   *
   * @return mixed
   *   Values for triggering events.
   */
  public function getWidgetValue(array $value_form) {
    return isset($value_form[0]['value']) ? $value_form[0]['value'] : $value_form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Checkbox::getWidgetValue public function Get values from widget settings for plugin. Overrides ConditionalFieldsHandlerBase::getWidgetValue
Checkbox::statesHandler public function @TODO: Different handlers for boolean and list fields. Overrides ConditionalFieldsHandlersPluginInterface::statesHandler
ConditionalFieldsHandlerBase::getConditionValues public function Get values form the condition options Overrides ConditionalFieldsHandlersPluginInterface::getConditionValues