Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event subscriber to include situation updates for full banner width alerts #16883

Merged
merged 15 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Drupal\va_gov_api\EventSubscriber;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Add "situation_update" paragraph data to the "/jsonapi/banner-alerts" route.
*/
class FullWidthBannerAlertSubscriber implements EventSubscriberInterface {

/**
* The entity type manager.
*/
private EntityTypeManagerInterface $entityTypeManager;

/**
* Constructor.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::RESPONSE => 'onKernelResponse',
];
}

/**
* Modify response to add "field_situation_updates" paragraph data.
*/
public function onKernelResponse(ResponseEvent $event): void {
// Only modify the "/jsonapi/banner-alerts" route.
$request = $event->getRequest();
if ($request->get('_route') !== 'va_gov_api.banner_alerts') {
return;
}

$response = $event->getResponse();
$decoded_content = json_decode($response->getContent(), TRUE);
$base_domain = $request->getSchemeAndHttpHost();

// Loop through data array and add "field_situation_updates" paragraph data.
foreach ($decoded_content['data'] as $value) {
if ($situation_updates_meta = $value['relationships']['field_situation_updates']['data']) {
foreach ($situation_updates_meta as $situation_update_meta) {
$situation_update_id = $situation_update_meta['meta']['drupal_internal__target_id'];
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $this->entityTypeManager->getStorage('paragraph')->load($situation_update_id);
$revision_id = $paragraph->getRevisionId();
$paragraph_type = $paragraph->getType();
$uuid = $paragraph->uuid();

$paragraphData = [
'type' => 'paragraph--' . $paragraph_type,
'id' => $uuid,
'links' => [
'self' => [
'href' => "$base_domain/jsonapi/paragraph/$paragraph_type/$uuid?resourceVersion=id%3A$revision_id",
],
],
'attributes' => [
'drupal_internal__id' => $paragraph->id(),
'drupal_internal__revision_id' => $revision_id,
'parent_id' => $paragraph->get('parent_id')->getValue()[0],
'field_datetime_range_timezone' => $paragraph->get('field_datetime_range_timezone')->getValue()[0],
'field_send_email_to_subscribers' => $paragraph->get('field_send_email_to_subscribers')->getValue()[0],
'field_wysiwyg' => $paragraph->get('field_wysiwyg')->getValue()[0],
],
'relationships' => [],
];

$decoded_content['included'][] = $paragraphData;
}
}
}

$content = json_encode($decoded_content);
$response->setContent($content);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Drupal\va_gov_api\Field;

use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;

/**
* Provides a computed breadcrumbs field item list.
*/
class ComputedSituationUpdatesItemList extends FieldItemList {

use ComputedItemListTrait;

/**
* {@inheritdoc}
*/
protected function computeValue() {
$parent = $this->getEntity();
if ($parent->isNew()) {
return;
}

// Get paragraph ids from 'field_situation_updates' field.
$situation_updates = $parent->get('field_situation_updates')->getValue();
$situation_update_ids = array_column($situation_updates, 'target_id');

// Load paragraph entities.
$paragraphs = \Drupal::entityTypeManager()
->getStorage('paragraph')
->loadMultiple($situation_update_ids);

foreach ($paragraphs as $key => $paragraph) {
$paragraphData = [
'revision_id' => $paragraph->getRevisionId(),
'paragraph_type' => $paragraph->getType(),
'uuid' => $paragraph->uuid(),
'field_datetime_range_timezone' => $paragraph->get('field_datetime_range_timezone')
->getValue()[0],
'field_send_email_to_subscribers' => $paragraph->get('field_send_email_to_subscribers')
->getValue()[0],
'field_wysiwyg' => $paragraph->get('field_wysiwyg')->getValue()[0],
];

$this->list[$key] = $this->createItem($key, $paragraphData);
}
}

}
29 changes: 29 additions & 0 deletions docroot/modules/custom/va_gov_api/va_gov_api.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @file
* Defines the hooks for va_gov_api module.
*/

//use Drupal\Core\Entity\EntityTypeInterface;
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
//use Drupal\Core\Field\BaseFieldDefinition;
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
//use Drupal\Core\Field\FieldStorageDefinitionInterface;
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.InvalidEndChar> reported by reviewdog 🐶
Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses

alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.InvalidEndChar> reported by reviewdog 🐶
Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses


/**
* Implements hook_entity_bundle_field_info().
*
* The hook_event_dispatcher module has this hook listed as a todo.
*
* @see core_event_dispatcher.module#L26
*/
//function va_gov_api_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [PHP_CodeSniffer] <Drupal.Files.LineLength.TooLong> reported by reviewdog 🐶
Line exceeds 80 characters; contains 122 characters

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.NoSpaceBefore> reported by reviewdog 🐶
No space found before comment text; expected "// function va_gov_api_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {" but found "//function va_gov_api_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.NotCapital> reported by reviewdog 🐶
Inline comments must start with a capital letter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [PHP_CodeSniffer] <Drupal.Files.LineLength.TooLong> reported by reviewdog 🐶
Line exceeds 80 characters; contains 122 characters

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.NoSpaceBefore> reported by reviewdog 🐶
No space found before comment text; expected "// function va_gov_api_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {" but found "//function va_gov_api_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [PHP_CodeSniffer] <Drupal.Commenting.InlineComment.NotCapital> reported by reviewdog 🐶
Inline comments must start with a capital letter

// if ($bundle === 'full_width_banner_alert') {
// $base_field_definitions['computed_situations_updates'] = BaseFieldDefinition::create('map')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [PHP_CodeSniffer] <Drupal.Files.LineLength.TooLong> reported by reviewdog 🐶
Line exceeds 80 characters; contains 97 characters

alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [PHP_CodeSniffer] <Drupal.Files.LineLength.TooLong> reported by reviewdog 🐶
Line exceeds 80 characters; contains 97 characters

alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
// ->setName('computed_situations_updates')
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
// ->setLabel(t('Computed Situation Updates'))
// ->setComputed(TRUE)
// ->setClass('\Drupal\va_gov_api\Field\ComputedSituationUpdatesItemList')
// ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
// }
// return $base_field_definitions;
//}
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
alexfinnarn marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 8 additions & 3 deletions docroot/modules/custom/va_gov_api/va_gov_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ parameters:
next_jsonapi.size_max: 2000
services:
va_gov_api.add_js_to_ui:
class: Drupal\va_gov_api\EventSubscriber\AddJsEventSubscriber
arguments: ['@request_stack']
tags:
class: Drupal\va_gov_api\EventSubscriber\AddJsEventSubscriber
arguments: ['@request_stack']
tags:
- { name: event_subscriber }
va_gov_api.full_width_banner_alert_subscriber:
class: Drupal\va_gov_api\EventSubscriber\FullWidthBannerAlertSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
Loading