Loading src/ConditionalFieldsFormHelper.php +21 −8 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\conditional_fields; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Field\WidgetBase; Loading Loading @@ -69,7 +70,7 @@ class ConditionalFieldsFormHelper { $options['selector'] = self::getSelector($options['selector'], $dependee_form_field); $state = self::getState($form_display, $dependee, $dependee_form_field, $options); $state = self::getState($form_display, $dependee, $dependee_form_field, $options, $form_state); // Add validation callback to element if the dependency can be evaluated. if (in_array($options['condition'], [ Loading Loading @@ -145,12 +146,16 @@ class ConditionalFieldsFormHelper { * Nested array of control field. * @param array $options * Settings of dependency. * * @param FormStateInterface $form_state * Form state interface * @return array * List of states. * * @see hook_get_state */ protected static function getState(EntityFormDisplayInterface $form_display, $dependee, $dependee_form_field, $options) { protected static function getState(EntityFormDisplayInterface $form_display, $dependee, $dependee_form_field, $options, $form_state = NULL) { $state = []; /** @var \Drupal\conditional_fields\ConditionalFieldsHandlersManager $type */ $type = \Drupal::service('plugin.manager.conditional_fields_handlers'); if ($options['condition'] != 'value') { Loading @@ -160,7 +165,7 @@ class ConditionalFieldsFormHelper { else { $field_name = explode('[', $dependee_form_field['#name']); $dependee_form_state = isset($dependee_form_field['#field_parents'], $field_name[0], $form_state) ? WidgetBase::getWidgetState($dependee_form_field['#field_parents'], $field_name[0], $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']; Loading Loading @@ -283,12 +288,19 @@ class ConditionalFieldsFormHelper { unset($input_state['add_more']); } $input_state = (is_null($input_state)) ? [] : $input_state; if (isset($dependent['field_parents'][0])) { $field = FieldStorageConfig::loadByName($form['#entity_type'], $dependent['field_parents'][0]); } else { $field = null; } if (empty($input_state)) { if (isset($element['widget']['#title'])) { $title = $element['widget']['#title']; } elseif (isset($dependent['field_parents'][0])) { $title = $dependent['field_parents'][0]; } elseif ($field) { $title = $field->getName(); } $form_state->setError($element, t('%name is required.', ['%name' => $title])); Loading Loading @@ -324,7 +336,7 @@ class ConditionalFieldsFormHelper { // element. This also means that so there can be multiple errors on the // same field (even though Drupal doesn't support multiple errors on the // same element). if (strpos($name, $error_key) === 0) { if (strpos((string)$name, $error_key) === 0) { $field_errors[$name] = $error; } } Loading Loading @@ -463,7 +475,7 @@ class ConditionalFieldsFormHelper { // dependent fields, and then reinstate remaining errors and messages. $errors = array_diff_assoc((array) $form_state->getErrors(), $untriggered_dependents_errors); $form_state->clearErrors(); $error_messages = drupal_get_messages('error'); $error_messages = \Drupal::messenger()->messagesByType('error'); $removed_messages = array_values($untriggered_dependents_errors); // Reinstate remaining errors. Loading @@ -477,8 +489,9 @@ class ConditionalFieldsFormHelper { // Reinstate remaining error messages (which, at this point, // are messages that were originated outside of the validation process). if (!empty($error_messages['error'])) { foreach (array_diff($error_messages['error'], $removed_messages) as $message) { drupal_set_message($message, 'error'); $error_messages_array = $error_messages['error'] instanceof MarkupInterface ? $error_messages['error']->jsonSerialize() : $error_messages['error']; foreach (array_diff($error_messages_array, $removed_messages) as $message) { \Drupal::messenger()->addMessage( $message, 'error' ); } } } Loading Loading
src/ConditionalFieldsFormHelper.php +21 −8 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\conditional_fields; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Field\WidgetBase; Loading Loading @@ -69,7 +70,7 @@ class ConditionalFieldsFormHelper { $options['selector'] = self::getSelector($options['selector'], $dependee_form_field); $state = self::getState($form_display, $dependee, $dependee_form_field, $options); $state = self::getState($form_display, $dependee, $dependee_form_field, $options, $form_state); // Add validation callback to element if the dependency can be evaluated. if (in_array($options['condition'], [ Loading Loading @@ -145,12 +146,16 @@ class ConditionalFieldsFormHelper { * Nested array of control field. * @param array $options * Settings of dependency. * * @param FormStateInterface $form_state * Form state interface * @return array * List of states. * * @see hook_get_state */ protected static function getState(EntityFormDisplayInterface $form_display, $dependee, $dependee_form_field, $options) { protected static function getState(EntityFormDisplayInterface $form_display, $dependee, $dependee_form_field, $options, $form_state = NULL) { $state = []; /** @var \Drupal\conditional_fields\ConditionalFieldsHandlersManager $type */ $type = \Drupal::service('plugin.manager.conditional_fields_handlers'); if ($options['condition'] != 'value') { Loading @@ -160,7 +165,7 @@ class ConditionalFieldsFormHelper { else { $field_name = explode('[', $dependee_form_field['#name']); $dependee_form_state = isset($dependee_form_field['#field_parents'], $field_name[0], $form_state) ? WidgetBase::getWidgetState($dependee_form_field['#field_parents'], $field_name[0], $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']; Loading Loading @@ -283,12 +288,19 @@ class ConditionalFieldsFormHelper { unset($input_state['add_more']); } $input_state = (is_null($input_state)) ? [] : $input_state; if (isset($dependent['field_parents'][0])) { $field = FieldStorageConfig::loadByName($form['#entity_type'], $dependent['field_parents'][0]); } else { $field = null; } if (empty($input_state)) { if (isset($element['widget']['#title'])) { $title = $element['widget']['#title']; } elseif (isset($dependent['field_parents'][0])) { $title = $dependent['field_parents'][0]; } elseif ($field) { $title = $field->getName(); } $form_state->setError($element, t('%name is required.', ['%name' => $title])); Loading Loading @@ -324,7 +336,7 @@ class ConditionalFieldsFormHelper { // element. This also means that so there can be multiple errors on the // same field (even though Drupal doesn't support multiple errors on the // same element). if (strpos($name, $error_key) === 0) { if (strpos((string)$name, $error_key) === 0) { $field_errors[$name] = $error; } } Loading Loading @@ -463,7 +475,7 @@ class ConditionalFieldsFormHelper { // dependent fields, and then reinstate remaining errors and messages. $errors = array_diff_assoc((array) $form_state->getErrors(), $untriggered_dependents_errors); $form_state->clearErrors(); $error_messages = drupal_get_messages('error'); $error_messages = \Drupal::messenger()->messagesByType('error'); $removed_messages = array_values($untriggered_dependents_errors); // Reinstate remaining errors. Loading @@ -477,8 +489,9 @@ class ConditionalFieldsFormHelper { // Reinstate remaining error messages (which, at this point, // are messages that were originated outside of the validation process). if (!empty($error_messages['error'])) { foreach (array_diff($error_messages['error'], $removed_messages) as $message) { drupal_set_message($message, 'error'); $error_messages_array = $error_messages['error'] instanceof MarkupInterface ? $error_messages['error']->jsonSerialize() : $error_messages['error']; foreach (array_diff($error_messages_array, $removed_messages) as $message) { \Drupal::messenger()->addMessage( $message, 'error' ); } } } Loading