Same name in other branches
  1. 8.x-1.x src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase::applyState()

Overrides FieldStateInterface::applyState

File

src/FieldStateBase.php, line 70

Class

FieldStateBase
Provides a base class for field staes.

Namespace

Drupal\field_states_ui

Code

public function applyState(array &$states, FormStateInterface $form_state, array $context, array $element, array $parents = NULL) {
    $target_field = $form_state->getFormObject()
        ->getFormDisplay($form_state)
        ->getComponent($this->configuration['target']);
    // If dealing with a field on an Inline Entity Form or a Field Collection
    // have to include the field parents in the selector.
    if (!empty($parents)) {
        $target = array_shift($parents) . '[' . implode('][', $parents) . '][' . $this->configuration['target'] . ']';
    }
    else {
        $target = $this->configuration['target'];
    }
    switch ($target_field['type']) {
        case 'options_select':
            $selector = "select[name^='{$target}']";
            break;
        default:
            $selector = ":input[name^='{$target}']";
            break;
    }
    $states[$this->pluginDefinition['id']][] = [
        $selector => [
            $this->configuration['comparison'] => $this->configuration['value'],
        ],
    ];
    return TRUE;
}