Skip to content

Commit

Permalink
slight refactor and scaffold form node plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rloos289 committed Nov 28, 2023
1 parent 60f8288 commit 68a98c0
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 12 deletions.
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
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 [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setCustomData($entity) {
'intro' => $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
@@ -0,0 +1,36 @@
<?php

namespace Drupal\sfgov_api\Plugin\SfgApi\Paragraph;

/**
* Plugin implementation of the sfgov_api.
*
* @SfgApi(
* id = "paragraph_form_io",
* title = @Translation("Paragraph form_io"),
* bundle = "form_io",
* wag_bundle = "form_io",
* entity_id = {},
* langcode = {},
* )
*/
class FormIo extends SfgApiParagraphBase {

/**
* {@inheritDoc}
*/
public function setCustomData($entity) {
return [
'formio_data_source' => $entity->get('field_formio_data_source')->value,
// @todo this plugin is incomplete
// 'field_custom_form_strings' => $entity->get('field_custom_form_strings')->value,
// 'field_form_strings' => $entity->get('field_form_strings')->value,
// 'field_formio_confirmation_url' => $entity->get('field_formio_confirmation_url')->value,
// 'field_formio_json_content' => $entity->get('field_formio_json_content')->value,
// 'field_formio_page_layout' => $entity->get('field_formio_page_layout')->value,
// 'field_formio_render_options' => $entity->get('field_formio_render_options')->value,
// 'field_get_formio_strings' => $entity->get('field_get_formio_strings')->value,
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* id = "paragraph_process_step",
* title = @Translation("Paragraph Process Step"),
* bundle = "process_step",
* wag_bundle = "cost",
* wag_bundle = "step",
* entity_id = {},
* langcode = {},
* )
Expand All @@ -30,7 +30,7 @@ public function setCustomData($entity) {
'optional' => $entity->get('field_process_optional')->value,
'time' => $entity->get('field_text_time')->value,
'step_description' => $entity->get('field_process_step_description')->value,
'cost' => $this->getReferencedData($entity->get('field_cost')->referencedEntities(), 'cost'),
'cost' => $this->getReferencedData($entity->get('field_cost')->referencedEntities()),
// Stream field referencing not currently supported by Wagtail.
'related_content_transactions' => $this->getReferencedEntity($entity->get('field_transaction')->referencedEntities()),
];
Expand Down
10 changes: 5 additions & 5 deletions web/modules/custom/sfgov_api/src/Templates/PluginTemplate.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class {{ bundle_camelize }} extends SfgApi{{ entity_type_ucfirst }}Base {
return [
{% if entity_fields %}
// Notes:
- Fields below are just a starting point, refine these down to the fields you actually want.
- The ->value function won't work for all fields and is just there to kickstart the process.
- Make sure to manually double check and update the wag_bundle in the annotation above.
- To small adjustments to the data only relevant to this entity, you add functions to this plugin.
- Look at ApiFieldHelperTrait.php for broad functions that can be used across all entities (like entity references).
// - Fields below are just a starting point, refine these down to the fields you actually want.
// - The ->value function won't work for all fields and is just there to kickstart the process.
// - Make sure to manually double check and update the wag_bundle in the annotation above.
// - To small adjustments to the data only relevant to this entity, you add functions to this plugin.
// - Look at ApiFieldHelperTrait.php for broad functions that can be used across all entities (like entity references).
{% for entity_field in entity_fields %}
'{{ entity_field }}' => $entity->get('{{ entity_field }}')->value,
{% endfor %}
Expand Down

0 comments on commit 68a98c0

Please sign in to comment.