Skip to content

Commit

Permalink
small edits to sfgov api
Browse files Browse the repository at this point in the history
  • Loading branch information
rloos289 committed Dec 4, 2023
1 parent 52a2274 commit cc98b35
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,19 @@ public function pushEntity($entity_type, $bundle, $langcode, $entity_id, $option
$payload = $this->sfgApiPluginManager->fetchPayload($plugin_label, $langcode, $entity_id);

if (empty($payload->getPayloadData())) {
$message = $this->t('No @entity_type of type @bundle with ID @entity_id in langcode @langcode found.', [
'@entity_type' => $entity_type,
'@bundle' => $bundle,
'@entity_id' => $entity_id,
'@langcode' => $langcode,
]);
// Try to send an error message from the payload since it will be more
// specific. If that doesn't work, send a generic message.
if ($payload_error = $payload->getErrors()) {
$message = $payload_error[0]['message'];
}
else {
$message = $this->t('No @entity_type of type @bundle with ID @entity_id in langcode @langcode found.', [
'@entity_type' => $entity_type,
'@bundle' => $bundle,
'@entity_id' => $entity_id,
'@langcode' => $langcode,
]);
}
return $this->output()->writeln($message);
}

Expand Down
4 changes: 2 additions & 2 deletions web/modules/custom/sfgov_api/src/Payload/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function setMetadata() {
$entity = $this->entity;
if (!empty($entity)) {
$metadata = [
'drupal_id' => $entity->id(),
'drupal_id' => (int) $entity->id(),
'entity_type' => $entity->getEntityTypeId(),
'bundle' => $entity->bundle(),
'langcode' => $this->requestedLangcode,
Expand All @@ -180,7 +180,7 @@ public function setStub() {
if (!empty($entity)) {
if ($entity instanceof Node) {
$stub = [
'parent_id' => $this->baseData['parent_id'],
'parent_id' => (int) $this->baseData['parent_id'],
'title' => $this->baseData['title'],
'slug' => $this->baseData['slug'],
'wag_bundle' => $this->wagBundle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ trait ApiFieldHelperTrait {
*
* @param array $entities
* An array of entities.
* @param string $wag_entity_type
* The wagtail entity type.
*
* @return array
* An array of entity data.
*/
public function getReferencedData(array $entities, $wag_entity_type) {
public function getReferencedData(array $entities) {
$sfgov_api_plugin_manager = \Drupal::service('plugin.manager.sfgov_api');
$available_plugins = $sfgov_api_plugin_manager->getDefinitions();

Expand Down Expand Up @@ -55,7 +53,7 @@ public function getReferencedData(array $entities, $wag_entity_type) {
'entity_id' => $entity->id(),
]);
$entities_data[] = [
'type' => $wag_entity_type,
'type' => $plugin->pluginDefinition['wag_bundle'],
'value' => $plugin->getPayload()->getPayloadData(),
];
}
Expand Down Expand Up @@ -142,4 +140,24 @@ public function convertDateFromFormat(string $input_format, string $value) {
return $drupalDatetime->format('Y-m-d');
}

/**
* Edit a field value based on a provided map.
*
* @param string $value
* The value from drupal to edit.
* @param array $value_map
* An array of values to map drupal_value => wagtail_value.
*
* @return string
* The edited value that wagtail expects.
*/
public function editFieldValue($value, $value_map) {
if (array_key_exists($value, $value_map)) {
return $value_map[$value];
}
else {
return $value;
}
}

}
26 changes: 1 addition & 25 deletions web/modules/custom/sfgov_api/src/Plugin/SfgApi/Media/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Drupal\sfgov_api\Plugin\SfgApi\Media;

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

/**
* Plugin implementation of the sfgov_api.
*
Expand All @@ -19,33 +16,12 @@
*/
class Image extends SfgApiMediaBase {

use ApiFieldHelperTrait;
use StringTranslationTrait;

/**
* {@inheritDoc}
*/
public function setCustomData($entity) {
$referenced_file = $entity->get('field_media_image')->referencedEntities()[0];
$file_uri = $referenced_file->getFileUri();
$file_path = \Drupal::service('file_system')->realpath($file_uri);
$referenced_file->getFilename();

$custom_data = [
'title' => $entity->get('name')->value,
'file' => $file_path,
];

if (!$file_path) {
$message = $this->t('No base file found for @entity_type of type @bundle with id @entity_id in langcode @langcode', [
'@entity_type' => $entity->getEntityTypeId(),
'@bundle' => $this->getBundle(),
'@entity_id' => $entity->id(),
'@langcode' => $this->configuration['langcode'],
]);
$this->addPluginError('No file', $message);
}

$custom_data = [];
return $custom_data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Drupal\sfgov_api\Plugin\SfgApi\Media;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\sfgov_api\Plugin\SfgApi\ApiFieldHelperTrait;
use Drupal\sfgov_api\SfgApiPluginBase;

/**
* Base class for sfgov_api plugins.
*/
abstract class SfgApiMediaBase extends SfgApiPluginBase {

use ApiFieldHelperTrait;
use StringTranslationTrait;

/**
* {@inheritDoc}
*/
Expand All @@ -18,8 +23,24 @@ abstract class SfgApiMediaBase extends SfgApiPluginBase {
* {@inheritDoc}
*/
public function setBaseData($media) {
$referenced_file = $media->get('field_media_image')->referencedEntities()[0];
$file_uri = $referenced_file->getFileUri();
$file_path = \Drupal::service('file_system')->realpath($file_uri);
$referenced_file->getFilename();

$base_data = [
'title' => $media->get('name')->value,
'file' => $file_path,
];

$base_data = [];
if (!$file_path) {
$message = $this->t('No base file found for media of type @bundle with id @entity_id in langcode @langcode', [
'@bundle' => $this->getBundle(),
'@entity_id' => $media->id(),
'@langcode' => $this->configuration['langcode'],
]);
$this->addPluginError('No file', $message);
}
return $base_data;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\sfgov_api\Plugin\SfgApi\Node;

use Drupal\sfgov_api\Plugin\SfgApi\ApiFieldHelperTrait;

/**
* Plugin implementation of the sfgov_api.
*
* @SfgApi(
* id = "node_form_confirmation_page",
* title = @Translation("Node form_confirmation_page"),
* bundle = "form_confirmation_page",
* wag_bundle = "form_confirmation_page",
* entity_id = {},
* langcode = {},
* )
*/
class FormConfirmationPage extends SfgApiNodeBase {

use ApiFieldHelperTrait;

/**
* {@inheritDoc}
*/
public function setCustomData($entity) {
return [
// @todo this plugin is incomplete
'body' => $entity->get('body')->value,
'field_bann' => $entity->get('field_bann')->value,
'field_banner_color' => $entity->get('field_banner_color')->value,
'field_banner_image' => $entity->get('field_banner_image')->value,
'field_confirmation_sidebar' => $entity->get('field_confirmation_sidebar')->value,
'field_departments' => $entity->get('field_departments')->value,
'field_description' => $entity->get('field_description')->value,
// 'field_form_confirm_page_slug' => $entity->get('field_form_confirm_page_slug')->value,
// 'field_related_content_single' => $entity->get('field_related_content_single')->value,
// 'field_step' => $entity->get('field_step')->value,
];
}

}
60 changes: 60 additions & 0 deletions web/modules/custom/sfgov_api/src/Plugin/SfgApi/Node/FormPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Drupal\sfgov_api\Plugin\SfgApi\Node;

use Drupal\node\Entity\Node;
use Drupal\sfgov_api\Plugin\SfgApi\ApiFieldHelperTrait;

/**
* Plugin implementation of the sfgov_api.
*
* @SfgApi(
* id = "node_form_page",
* title = @Translation("Node form_page"),
* bundle = "form_page",
* wag_bundle = "form_page",
* entity_id = {},
* langcode = {},
* )
*/
class FormPage extends SfgApiNodeBase {

use ApiFieldHelperTrait;

/**
* {@inheritDoc}
*/
public function setCustomData($entity) {
$formio_data_source = $this->getReferencedData($entity->get('field_form_id')->referencedEntities());
return [
// @todo this plugin is incomplete
// 'field_formio_json_content' => $entity->get('field_formio_json_content')->value,
// 'field_form_id' => $entity->get('field_form_id')->value,
// 'field_intro_text' => $entity->get('field_intro_text')->value,
'data_source' => $formio_data_source[0]['value']['formio_data_source'],
'confirmation_page' => $this->getReferencedData([$this->getFormConfirmationPage($entity->id())]),
];
}

/**
* Get the form confirmation page for a given form. WIP.
*
* @param int $form_id
* The form id.
*
* @return array
* The form confirmation page.
*/
public function getFormConfirmationPage($form_id) {
$query = \Drupal::entityQuery('node')
->condition('type', 'form_confirmation_page')
->condition('field_related_content_single', $form_id);
$nids = $query->execute();
if (count($nids) > 0) {
$nid = array_shift($nids);
return Node::load($nid);
}
return [];
}

}
20 changes: 1 addition & 19 deletions web/modules/custom/sfgov_api/src/Plugin/SfgApi/Node/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,9 @@ public function setCustomData($entity) {
'date' => $this->convertDateFromFormat('Y-m-d\TH:i:s', $entity->get('field_date')->value),
'abstract' => $entity->get('field_abstract')->value,
'body' => $entity->get('body')->value,
'news_type' => $this->fixNewsType($entity->get('field_news_type')->value),
'news_type' => $this->editFieldValue($entity->get('field_news_type')->value, ['news' => 'press_release']),
'image' => $this->getReferencedEntity($entity->get('field_image')->referencedEntities()),
];
}

/**
* Fix the news type. 'Press release' should be 'press_release'.
*
* @param string $value
* The value to fix.
*
* @return string
* The fixed value.
*/
public function fixNewsType($value) {
if ($value === 'press release') {
return 'press_release';
}
else {
return $value;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setBaseData($node) {
$alias = $path_alias_manager->getAliasByPath('/node/' . $node_id);
$wag_api_settings = \Drupal::config('sfgov_api.settings');
$base_data = [
'parent_id' => $wag_api_settings->get('wag_parent_' . $this->configuration['langcode']),
'parent_id' => (int) $wag_api_settings->get('wag_parent_' . $this->configuration['langcode']),
'title' => $node->getTitle(),
'slug' => basename($alias),
'aliases' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class StepByStep extends SfgApiNodeBase {
public function setCustomData($entity) {
return [
'description' => $entity->get('field_description')->value,
'intro' => $entity->get('field_intro_text')->value,
'intro' => (string) $entity->get('field_intro_text')->value,
// Entity Reference not currently supported by Wagtail.
'related_content_topics' => $this->getReferencedEntity($entity->get('field_topics')->referencedEntities()),
'steps' => $this->getReferencedData($entity->get('field_process_steps')->referencedEntities(), 'step'),
'steps' => $this->getReferencedData($entity->get('field_process_steps')->referencedEntities()),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\sfgov_api\Plugin\SfgApi\Paragraph;

use Drupal\sfgov_api\Plugin\SfgApi\ApiFieldHelperTrait;

/**
* Plugin implementation of the sfgov_api.
*
Expand All @@ -16,13 +18,15 @@
*/
class Cost extends SfgApiParagraphBase {

use ApiFieldHelperTrait;

/**
* {@inheritDoc}
*/
public function setCustomData($entity) {
return [
'description' => $entity->get('field_text')->value,
'cost' => $entity->get('field_cost_type')->value,
'cost' => $this->editFieldValue($entity->get('field_cost_type')->value, ['flat' => 'flat_fee']),
'flat_fee' => $entity->get('field_cost_flat_fee')->value,
'range' => [
'maximum' => $entity->get('field_cost_maximum')->value,
Expand Down
Loading

0 comments on commit cc98b35

Please sign in to comment.