Skip to content

Commit

Permalink
Feature/wag fixes (#1693)
Browse files Browse the repository at this point in the history
* add metadata to location content types

* remove  from fields

* add path for filtering by id

* add dates to metadata, file data to media

* fix metadata for paragraphs

---------

Co-authored-by: rloos289 <rloos289@gmail.com>
  • Loading branch information
aekong and rloos289 authored Jun 21, 2024
1 parent 1d35234 commit 17a823a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
4 changes: 4 additions & 0 deletions web/modules/custom/sfgov_api/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ an html page with the full error message (e.g. pushing a slug that already exist
Every node that that is pushed registers data to its table. If it has an error then it will
record the error id which can then be looked up in the `dw_migration_errors` table.

## Listing nodes
You can list all ids of a certain entity type and bundle by going to `/sfgov-api-info/entity/{entity_type}/{bundle}/id`
(eg `/sfgov-api-info/entity/node/step_by_step/id`)

# Wrangling Wagtail
**Shape of Wagtail Data**
If you need to see the shape of data in Wagtail, create that page, then go to its
Expand Down
11 changes: 11 additions & 0 deletions web/modules/custom/sfgov_api/sfgov_api.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ sfgov_api.api_viewer:
requirements:
_permission: 'administer site configuration'

sfgov_api.api_info:
path: '/sfgov-api-info/entity/{entity_type}/{bundle}/{filter}'
defaults:
_title: 'View entity info'
_controller: '\Drupal\sfgov_api\Controller\SfgApiController::viewEntityInfo'
entity_type: ''
bundle: ''
filter: ''
requirements:
_permission: 'administer site configuration'

sfgov_api.wagtail_api_credentials:
path: '/admin/config/system/wagtail-api-credentials'
defaults:
Expand Down
41 changes: 40 additions & 1 deletion web/modules/custom/sfgov_api/src/Controller/SfgApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\sfgov_api\SfgApiPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* Returns responses for sfgov_api routes.
Expand All @@ -19,11 +20,19 @@ class SfgApiController extends ControllerBase {
*/
protected $sfgApiPluginManager;

/**
* The entity type manager.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Constructs a new SfgApiController.
*/
public function __construct(SfgApiPluginManager $sfgApiPluginManager) {
public function __construct(SfgApiPluginManager $sfgApiPluginManager, EntityTypeManagerInterface $entityTypeManager) {
$this->sfgApiPluginManager = $sfgApiPluginManager;
$this->entityTypeManager = $entityTypeManager;
}

/**
Expand All @@ -32,6 +41,7 @@ public function __construct(SfgApiPluginManager $sfgApiPluginManager) {
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.sfgov_api'),
$container->get('entity_type.manager')
);
}

Expand Down Expand Up @@ -76,4 +86,33 @@ public function viewEntityData(string $shape, $langcode, string $entity_type, st
return new JsonResponse($display);
}

/**
* View the entity info for the associated arguments.
*/
public function viewEntityInfo($entity_type, $bundle, $filter) {
$display = [];
if (empty($entity_type)) {
$display[]['error'] = 'Please specify an entity type.';
}
if (empty($bundle)) {
$display[]['error'] = 'Please specify a bundle.';
}
if (empty($filter)) {
$display[]['error'] = 'Please specify a filter.';
}

if (empty($display)) {
if ($filter === 'id') {
$bundle_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('bundle');
$query = $this->entityTypeManager->getStorage($entity_type)->getQuery()
->accessCheck(FALSE)
->condition($bundle_key, $bundle);
$entity_ids = $query->execute();
$display = array_values($entity_ids);
}
}

return new JsonResponse($display);
}

}
8 changes: 8 additions & 0 deletions web/modules/custom/sfgov_api/src/Payload/PayloadBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\sfgov_api\Plugin\SfgApi\ApiFieldHelperTrait;

/**
* Class for Json Payloads to be sent to Wagtail.
*/
abstract class PayloadBase {

use StringTranslationTrait;
use ApiFieldHelperTrait;

/**
* The metadata used to support the payload.
Expand Down Expand Up @@ -142,9 +144,15 @@ public function setMetadata() {
'translations' => array_keys($entity->getTranslationLanguages()),
'wag_bundle' => $this->wagBundle,
'published' => $entity->isPublished(),
'created' => $this->convertTimestampToFormat($entity->getCreatedTime(), 'Y-m-d\TH:i:s'),
];
}

// All other entity types have a getChangedTime method except paragraphs.
if ($entity->getEntityTypeId() != 'paragraph') {
$metadata['changed'] = $this->convertTimestampToFormat($entity->getChangedTime(), 'Y-m-d\TH:i:s');
}

return $this->metadata = $metadata;
}

Expand Down
28 changes: 28 additions & 0 deletions web/modules/custom/sfgov_api/src/Payload/RawPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function initializeFieldTypeHandlers() {
'address' => [$this, 'handleAddressField'],
'smartdate' => [$this, 'handleSmartDateField'],
'office_hours' => [$this, 'handleOfficeHoursField'],
'file' => [$this, 'handleFileField'],
];
}

Expand Down Expand Up @@ -243,4 +244,31 @@ protected function handleOfficeHoursField($field_data) {
return $this->formatOfficeHours($field_data->getValue());
}

/**
* Handles file fields.
*
* @param mixed $field_data
* The field data.
*
* @return array
* The processed data.
*/
protected function handleFileField($field_data) {
$referenced_file = $field_data->referencedEntities()[0];
if (isset($referenced_file)) {
$file_uri = $referenced_file->getFileUri();

$data['target_id'] = $referenced_file->id();
$data['file'] = [
'filename' => $referenced_file->getFilename(),
'path' => \Drupal::service('file_system')->realpath($file_uri),
'uri' => $referenced_file->getFileUri(),
'fid' => $referenced_file->id(),
'filesize' => $referenced_file->getSize(),
'filemime' => $referenced_file->getMimeType(),
];

}
return $data;
}
}

1 comment on commit 17a823a

@aekong
Copy link
Collaborator Author

@aekong aekong commented on 17a823a Jun 21, 2024

Choose a reason for hiding this comment

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

Visit Site

Created multidev environment ci-20380 for sfgov.

Please sign in to comment.