diff --git a/web/modules/custom/sfgov_api/README.MD b/web/modules/custom/sfgov_api/README.MD index 11d14a99b..0c7e78f7c 100644 --- a/web/modules/custom/sfgov_api/README.MD +++ b/web/modules/custom/sfgov_api/README.MD @@ -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 diff --git a/web/modules/custom/sfgov_api/sfgov_api.routing.yml b/web/modules/custom/sfgov_api/sfgov_api.routing.yml index 6a5b100d1..a975a7bb8 100644 --- a/web/modules/custom/sfgov_api/sfgov_api.routing.yml +++ b/web/modules/custom/sfgov_api/sfgov_api.routing.yml @@ -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: diff --git a/web/modules/custom/sfgov_api/src/Controller/SfgApiController.php b/web/modules/custom/sfgov_api/src/Controller/SfgApiController.php index 8b585b94f..2f54536fa 100644 --- a/web/modules/custom/sfgov_api/src/Controller/SfgApiController.php +++ b/web/modules/custom/sfgov_api/src/Controller/SfgApiController.php @@ -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. @@ -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; } /** @@ -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') ); } @@ -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); + } + } diff --git a/web/modules/custom/sfgov_api/src/Payload/PayloadBase.php b/web/modules/custom/sfgov_api/src/Payload/PayloadBase.php index caf2cca88..adb9f9321 100644 --- a/web/modules/custom/sfgov_api/src/Payload/PayloadBase.php +++ b/web/modules/custom/sfgov_api/src/Payload/PayloadBase.php @@ -4,6 +4,7 @@ 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. @@ -11,6 +12,7 @@ abstract class PayloadBase { use StringTranslationTrait; + use ApiFieldHelperTrait; /** * The metadata used to support the payload. @@ -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; } diff --git a/web/modules/custom/sfgov_api/src/Payload/RawPayload.php b/web/modules/custom/sfgov_api/src/Payload/RawPayload.php index c20dbacb2..21c6cdbde 100644 --- a/web/modules/custom/sfgov_api/src/Payload/RawPayload.php +++ b/web/modules/custom/sfgov_api/src/Payload/RawPayload.php @@ -56,6 +56,7 @@ protected function initializeFieldTypeHandlers() { 'address' => [$this, 'handleAddressField'], 'smartdate' => [$this, 'handleSmartDateField'], 'office_hours' => [$this, 'handleOfficeHoursField'], + 'file' => [$this, 'handleFileField'], ]; } @@ -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; + } }