public function FieldStateBase::applyState in Field States UI 8.2
Same name and namespace in other branches
- 8 src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase::applyState()
Applies a field state to the field widget's form element.
Parameters
array $states: An array to hold states.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $context: An associative array containing the following key-value pairs:
- form: The form structure to which widgets are being attached. This may be a full form structure, or a sub-element of a larger form.
- widget: The widget plugin instance.
- items: The field values, as a \Drupal\Core\Field\FieldItemListInterface object.
- delta: The order of this item in the array of sub-elements (0, 1, n).
- default: A boolean indicating whether the form is being shown as a dummy form to set default values.
array $element: The field widget form element as constructed by hook_field_widget_form().
array $parents: The parents array from the element.
Return value
bool TRUE on success. FALSE if unable to calculate the field state.
Overrides FieldStateInterface::applyState
See also
\Drupal\Core\Field\WidgetBase::formSingleElement()
hook_field_widget_form_alter()
File
- src/
FieldStateBase.php, line 70
Class
- FieldStateBase
- Provides a base class for field staes.
Namespace
Drupal\field_states_uiCode
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;
}