You are here

protected function ConditionalFieldsFormHelper::getState in Conditional Fields 8

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

Get list of states for the pair from the options.

Parameters

string $dependee: Machine name of control field.

array $dependee_form_field: Nested array of control field.

array $options: Settings of dependency.

Return value

array List of states.

See also

hook_get_state

1 call to ConditionalFieldsFormHelper::getState()
ConditionalFieldsFormHelper::processDependeeFields in src/ConditionalFieldsFormHelper.php
Determine and register dependee field effects.

File

src/ConditionalFieldsFormHelper.php, line 183

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

protected function getState($dependee, $dependee_form_field, $options) {

  // @var EntityFormDisplayInterface $form_display
  $form_display = $this->form_state
    ->getFormObject()
    ->getFormDisplay($this->form_state);
  $state = [];
  if ($options['condition'] != 'value') {

    // Conditions different than "value" are always evaluated against TRUE.
    $state = [
      $options['state'] => [
        $options['selector'] => [
          $options['condition'] => TRUE,
        ],
      ],
    ];
  }
  else {
    $field_name = explode('[', $dependee_form_field['#name']);
    $dependee_form_state = isset($dependee_form_field['#field_parents'], $field_name[0], $this->form_state) ? WidgetBase::getWidgetState($dependee_form_field['#field_parents'], $field_name[0], $this->form_state) : NULL;
    $dependee_form_field['#original_name'] = $field_name[0];
    $dependee_display = $form_display
      ->getComponent($dependee);
    if (is_array($dependee_display) && array_key_exists('type', $dependee_display)) {
      $widget_id = $dependee_display['type'];
    }

    // @todo: Use field cardinality instead of number of values that was
    // selected on manage dependency tab. As temporary solution put cardinality
    // in $options. Format of #states depend on field widget and field
    // cardinality (it can be like value: string and value: [array]).
    if ($field_config = FieldStorageConfig::loadByName($form_display
      ->getTargetEntityTypeId(), $dependee)) {
      $options['field_cardinality'] = $field_config
        ->getCardinality();
    }

    // Execute special handler for fields that need further processing.
    // The handler has no return value. Modify the $state parameter by
    // reference if needed.
    if (isset($widget_id)) {
      $handler_id = 'states_handler_' . $widget_id;

      /** @var Drupal\conditional_fields\ConditionalFieldsHandlersPluginInterface $handler */
      $handler = $this->type
        ->createInstance($handler_id);
      $state = $handler
        ->statesHandler($dependee_form_field, $dependee_form_state, $options);
    }
    if (empty($state)) {

      // If states empty Default plugin.

      /** @var Drupal\conditional_fields\ConditionalFieldsHandlersPluginInterface $default_handler */
      $default_handler = $this->type
        ->createInstance('states_handler_default_state');
      $state = $default_handler
        ->statesHandler($dependee_form_field, $dependee_form_state, $options);
    }
  }
  return $state;
}