Skip to content

Commit

Permalink
VACMS-16348: Trying to get paragraph validation to work
Browse files Browse the repository at this point in the history
  • Loading branch information
omahane committed Jan 31, 2024
1 parent 26dabc9 commit fb814db
Showing 1 changed file with 62 additions and 39 deletions.
101 changes: 62 additions & 39 deletions docroot/modules/custom/va_gov_facilities/va_gov_facilities.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Drupal\Component\Render\FormattableMarkup;

use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\va_gov_facilities\EventSubscriber\FacilitiesSubscriber;

Expand Down Expand Up @@ -78,49 +77,73 @@ function _va_gov_facilities_populate_service_default_appt_text(FormStateInterfac
return $default_text;
}

/**
* Adds Validation to check for an empty Appointment lead-in field.
*
* @param array $element
* The element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function _va_gov_facilities_appt_intro_text_validation($element, FormStateInterface $form_state) {
$service_location = $form_state->getValue('field_service_location');
if (isset($service_location[0]['subform']['field_appt_intro_text_type'])) {
$appt_text_type = $service_location[0]['subform']['field_appt_intro_text_type'][0]['value'];
$custom_text = $service_location[0]['subform']['field_appt_intro_text_custom'][0]['value'];
if ($appt_text_type === 'customize_text' && $custom_text === '') {
$form_state->setErrorByName("field_service_location][0][subform][field_appt_intro_text_custom'", t('Custom text for appointment introduction text is required'));
}
}
}

/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function va_gov_facilities_field_widget_single_element_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
$field_definition = $context['items']->getFieldDefinition();
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$paragraph_entity_reference_field_name = $field_definition->getName();

if ($paragraph_entity_reference_field_name === "field_service_location") {
$widget_state = WidgetBase::getWidgetState($element['#field_parents'], 'field_service_location', $form_state);
$paragraph_instance = $widget_state['paragraphs'][$element['#delta']]['entity'];
$paragraph_type = $paragraph_instance->bundle();

if ($paragraph_type == 'service_location') {
// Set the default text for appointments.
$default_text = _va_gov_facilities_populate_service_default_appt_text($form_state) ?? '';
$description = new FormattableMarkup($default_text, []);

// Create the form element for the default text for appointments.
// Nest it under the select list about the type of text.
$element['subform']['field_appt_intro_text_type']['temp_default_text'] = [
'#type' => 'textarea',
'#title' => t('Default text'),
'#value' => $description,
'#weight' => 1,
'#disabled' => TRUE,
];

// Hide appointments custom text unless explicitly selected.
$selector = sprintf(':input[name="field_service_location[%d][subform][field_appt_intro_text_type]"]', $element['#delta']);

$element['subform']['field_appt_intro_text_custom']['#states'] = [
'visible' => [
$selector => ['value' => 'customize_text'],
],
];

// The default appointment text shows as a disabled form element.
$element['subform']['field_appt_intro_text_type']['temp_default_text']['#states'] = [
'visible' => [
$selector => ['value' => 'use_default_text'],
],
];
// $field_definition = $context['items']->getFieldDefinition();
// $paragraph_entity_reference_field_name = $field_definition->getName();
$paragraph_type = $element['#paragraph_type'] ?? '';

if ($paragraph_type === 'service_location') {

// Set the default text for appointments.
$default_text = _va_gov_facilities_populate_service_default_appt_text($form_state) ?? '';
$description = new FormattableMarkup($default_text, []);

// Create the form element for the default text for appointments.
// Nest it under the select list about the type of text.
$element['subform']['field_appt_intro_text_type']['temp_default_text'] = [
'#type' => 'textarea',
'#title' => t('Default text'),
'#value' => $description,
'#weight' => 1,
'#disabled' => TRUE,
];

// Hide appointments custom text unless explicitly selected.
$selector = sprintf(':input[name="field_service_location[%d][subform][field_appt_intro_text_type]"]', $element['#delta']);

$element['subform']['field_appt_intro_text_custom']['#states'] = [
'required' => [
$selector => ['value' => 'customize_text'],
],
'visible' => [
$selector => ['value' => 'customize_text'],
],

];

// The default appointment text shows as a disabled form element.
$element['subform']['field_appt_intro_text_type']['temp_default_text']['#states'] = [
'visible' => [
$selector => ['value' => 'use_default_text'],
],
];

// Make sure the custom text field is filled out (when option is selected).
if (isset($element['subform']['field_appt_intro_text_type'])) {
$element['subform']['field_appt_intro_text_custom']['widget']['#element_validate'][] = '_va_gov_facilities_appt_intro_text_validation';
}
}

Expand Down

0 comments on commit fb814db

Please sign in to comment.