From cee6d5889340009fdbc54a63ed31c601edb9c5d9 Mon Sep 17 00:00:00 2001 From: Christia Troyer Date: Thu, 5 Oct 2023 03:09:35 -0700 Subject: [PATCH] VACMS-15337: Update lock and resolve conflicts --- composer.lock | 2 +- .../EntityEventSubscriber.php.bak15242 | 395 ------------------ 2 files changed, 1 insertion(+), 396 deletions(-) delete mode 100644 docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php.bak15242 diff --git a/composer.lock b/composer.lock index 20de69f6830..c170d8f94a2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "94dcbe0df6c2e22ca014a2be3d094dcc", + "content-hash": "4b20106f4c2210b4ffd1c03873a45763", "packages": [ { "name": "alchemy/zippy", diff --git a/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php.bak15242 b/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php.bak15242 deleted file mode 100644 index f8cf88f66c4..00000000000 --- a/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php.bak15242 +++ /dev/null @@ -1,395 +0,0 @@ - 'alterNodeForm', - 'hook_event_dispatcher.form_base_taxonomy_term_form.alter' => 'alterTermForm', - 'hook_event_dispatcher.form_base_block_content_form.alter' => 'alterBlockContentForm', - EntityHookEvents::ENTITY_DELETE => 'entityDelete', - EntityHookEvents::ENTITY_INSERT => 'entityInsert', - EntityHookEvents::ENTITY_UPDATE => 'entityUpdate', - ]; - } - - /** - * Constructs the EventSubscriber object. - * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entityTypeManager. - * @param \Drupal\va_gov_user\Service\UserPermsService $user_perms_service - * The user perms service. - * @param \Drupal\va_gov_workflow\Service\WorkflowContentControl $workflow_content_control - * The workflow content control service. - * @param \Drupal\va_gov_workflow\Service\Flagger $flagger - * The vagov workflow flagger service. - * @param \Drupal\va_gov_notifications\Service\NotificationsManager $notifications_manager - * VA gov NotificationsManager service. - */ - public function __construct( - EntityTypeManagerInterface $entity_type_manager, - UserPermsService $user_perms_service, - WorkflowContentControl $workflow_content_control, - Flagger $flagger, - NotificationsManager $notifications_manager - ) { - $this->entityTypeManager = $entity_type_manager; - $this->userPermsService = $user_perms_service; - $this->workflowContentControl = $workflow_content_control; - $this->flagger = $flagger; - $this->notificationsManager = $notifications_manager; - } - - /** - * Entity update Event call. - * - * @param \Drupal\core_event_dispatcher\Event\Entity\EntityUpdateEvent $event - * The event. - */ - public function entityUpdate(EntityUpdateEvent $event): void { - $entity = $event->getEntity(); - $this->flagVaFormChanges($entity); - } - - /** - * Alters to be applied to all content types. - * - * @param \Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent $event - * The event. - */ - public function alterNodeForm(FormBaseAlterEvent $event) { - $form = &$event->getForm(); - $form_state = $event->getFormState(); - $form_id = $event->getFormId(); - if ($form_state->getFormObject() instanceof EntityFormInterface) { - $this->removeArchiveOption($event); - } - $this->requireRevisionMessage($form, $form_state, $form_id); - } - - /** - * Alters to be applied to all block content types. - * - * @param \Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent $event - * The event. - */ - public function alterBlockContentForm(FormBaseAlterEvent $event) { - $form = &$event->getForm(); - $form_state = $event->getFormState(); - $form_id = $event->getFormId(); - $this->requireRevisionMessage($form, $form_state, $form_id); - $this->removeArchiveOption($event); - } - - /** - * Alters to be applied to all content types. - * - * @param \Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent $event - * The event. - */ - public function alterTermForm(FormBaseAlterEvent $event) { - $form = &$event->getForm(); - $form_state = $event->getFormState(); - if ($form_state->getFormObject() instanceof EntityFormInterface) { - $this->removePreviousRevisionLogMessage($form); - } - } - - /** - * Prevent the previous revision message from persisting to new revision. - * - * This is due workflow not fully supporting terms and using a patch for - * issue #3047110 {@see https://drupal.org/i/3047110} - * - * @param array $form - * The Drupal form array by reference. - */ - protected function removePreviousRevisionLogMessage(array &$form): void { - if (isset($form['revision_log_message'])) { - $form["revision_log_message"]["widget"][0]["value"]["#default_value"] = ''; - } - } - - /** - * Removes archive option for non-admins on certain entity types. - * - * @param \Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent $event - * The event. - */ - private function removeArchiveOption(FormBaseAlterEvent $event) { - $form = &$event->getForm(); - $form_state = $event->getFormState(); - if ($form_state->getFormObject() instanceof ContentEntityForm) { - /** @var \Drupal\Core\Entity\EntityInterface $entity */ - $entity = $form_state->getFormObject()->getEntity(); - $entity_type = $entity->getEntityType()->id(); - $bundle = $entity->bundle(); - $is_admin = $this->userPermsService->hasAdminRole(); - $is_archiveable = $this->workflowContentControl->isBundleArchiveableByNonAdmins($bundle, $entity_type); - if (!$is_admin && !$is_archiveable) { - unset($form['moderation_state']['widget'][0]['state']['#options']['archived']); - } - } - } - - /** - * Flag VA Forms if certain changes are made. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * An entity of unknown type. - */ - protected function flagVaFormChanges(EntityInterface $entity) { - if ($entity->bundle() === 'va_form') { - $message_fields = $this->notificationsManager->buildMessageFields($entity); - if ($this->flagger->flagFieldChanged('field_va_form_title', 'changed_title', $entity, "The form title of this form changed from '@old' to '@new' in the Forms DB.")) { - $this->notificationsManager->send('va_form_changed_title', '#va-forms', $message_fields, 'slack'); - } - - if ($this->flagger->flagFieldChanged(['field_va_form_url', 'uri'], 'changed_filename', $entity, "The file name (URL) of this form changed from '@old' to '@new' in the Forms DB.")) { - $this->notificationsManager->send('va_form_changed_url', '#va-forms', $message_fields, 'slack'); - } - - if ($this->flagger->flagFieldChanged('field_va_form_deleted', 'deleted', $entity, "The form was marked as deleted in the Forms DB.")) { - $this->notificationsManager->send('va_form_deleted', '#va-forms', $message_fields, 'slack'); - } - } - } - - /** - * Entity Insert Event call. - * - * @param \Drupal\core_event_dispatcher\Event\Entity\EntityInsertEvent $event - * The event. - */ - public function entityInsert(EntityInsertEvent $event): void { - $entity = $event->getEntity(); - - if ($entity->getEntityTypeId() === 'flagging') { - // A flag is being added. - $this->flagger->logFlagOperation($entity, 'create'); - } - elseif ($entity->bundle() === 'va_form') { - $flagged = $this->flagger->flagNew('new_form', $entity, "This VA Form was added to the Forms DB."); - if ($flagged) { - $message_fields = $this->notificationsManager->buildMessageFields($entity); - $this->notificationsManager->send('va_form_new_form', '#va-forms', $message_fields, 'slack'); - } - } - } - - /** - * Entity Delete Event call. - * - * @param \Drupal\core_event_dispatcher\Event\Entity\EntityDeleteEvent $event - * The event. - */ - public function entityDelete(EntityDeleteEvent $event): void { - $entity = $event->getEntity(); - - if ($entity->getEntityTypeId() === 'flagging') { - // A flag is being deleted. - $this->flagger->logFlagOperation($entity, 'delete'); - } - } - - /** - * Adds Validation to check revision log message is added. - * - * @param array $form - * The exposed widget form array. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The form state. - * @param string $form_id - * The form id. - */ - public function requireRevisionMessage(array &$form, FormStateInterface &$form_state, $form_id): void { - // Hide the checkbox that lets user opt into making a revision. - $formObject = $form_state->getFormObject(); - if ($formObject instanceof EntityFormInterface) { - $entity = $formObject->getEntity(); - if ($entity->bundle() === 'promo') { - // Hide revision checkbox on Promo blocks. - $form["revision_information"]["#access"] = TRUE; - unset($form['revision_log']['#states']); - $form["revision"]["#access"] = FALSE; - $form["revision_log"]["#access"] = TRUE; - } - else { - // Hide revision checkbox on all other custom blocks. - $form["revision_information"]["#attributes"]['class'][] = 'visually-hidden'; - $this->bypassRevisionLogValidationOnIef($form, $form_state); - } - } - - // Make revision log required. - $form['revision_log']['#required'] = TRUE; - $form['revision_log']['widget']['#required'] = TRUE; - $form['revision_log']['widget'][0]['#required'] = TRUE; - $form['revision_log']['widget'][0]['value']['#required'] = TRUE; - $form['#validate'][] = '_va_gov_workflow_validate_required_revision_message'; - } - - /** - * Bypasses required revision log for validation for Inline Entity Forms. - * - * @param array $form - * The form array by reference. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The form state. - */ - protected function bypassRevisionLogValidationOnIef(array &$form, FormStateInterface $form_state): void { - /** @var \Drupal\node\NodeInterface $node **/ - $node = $form_state->getFormObject()->getEntity(); - if (!$node instanceof NodeInterface) { - return; - } - $ief_fields = $this->getIefTypeFields($node); - $operation_button_map = [ - 'field_widget_edit' => 'edit_button', - 'field_widget_remove' => 'remove_button', - 'field_widget_replace' => 'replace_button', - ]; - - foreach ($ief_fields as $ief_field_name => $widget_operations) { - $widget_operations = array_intersect_key($operation_button_map, $widget_operations); - // Stop the node form validation from firing on ief operation buttons. - $current_widgets = $form[$ief_field_name]['widget']['current'] ?? []; - foreach ($current_widgets as $key => $widget_details) { - // The array contains a mix of numeric keys and named keys. The numeric - // are the ones with button actions that we must intercept. - if (is_numeric($key)) { - foreach ($widget_operations as $widget_button) { - if ($widget_button && isset($form[$ief_field_name]['widget']['current'][$key]['actions'][$widget_button])) { - $form[$ief_field_name]['widget']['current'][$key]['actions'][$widget_button]['#limit_validation_errors'] = [[$ief_field_name]]; - } - } - } - } - } - - } - - /** - * Get all fields on node that are IEF fields that result node edit/save. - * - * @param \Drupal\node\NodeInterface $node - * The node to pull the fields from. - * - * @return array - * An array whose elements look like 'field_name' => [widget operations]. - */ - protected function getIefTypeFields(NodeInterface $node): array { - $ief_fields = []; - // Load all of the form displays for the given bundle. - /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form_displays */ - $form_displays = $this->entityTypeManager->getStorage('entity_form_display')->loadByProperties([ - 'targetEntityType' => 'node', - 'bundle' => $node->bundle(), - ]); - $form_display = reset($form_displays); - $field_displays = $form_display->toArray(); - - foreach ($field_displays['content'] as $field_name => $field_display) { - if ($this->isNodeIef($node, $field_name)) { - $operations = [ - 'field_widget_edit' => !empty($field_display['settings']['field_widget_edit']), - 'field_widget_remove' => !empty($field_display['settings']['field_widget_remove']), - 'field_widget_replace' => !empty($field_display['settings']['field_widget_replace']), - ]; - $operations = array_filter($operations); - $ief_fields[$field_name] = $operations; - } - } - - return $ief_fields; - } - - /** - * Checks to see if a field is an entity reference that targets a node. - * - * @param \Drupal\node\NodeInterface $node - * The node object. - * @param string $field_name - * The machine name of the field being checked. - * - * @return bool - * TRUE if it is an ief field targeting a node, FALSE otherwise. - */ - protected function isNodeIef(NodeInterface $node, $field_name): bool { - $field_definition = $node->getFieldDefinition($field_name); - if (empty($field_definition)) { - return FALSE; - } - $fieldType = $field_definition->getType(); - $field_types_for_ief = [ - 'entity_reference', - 'entity_reference_revisions', - ]; - $target_type = $field_definition->getItemDefinition()->getSettings()['target_type'] ?? ''; - - return (in_array($fieldType, $field_types_for_ief)) && ($target_type === "node"); - } - -}