-
Notifications
You must be signed in to change notification settings - Fork 70
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
VACMS-14000: Adds and populates meta tags on VBA Facility #16395
Changes from 3 commits
6dfe82a
cdc02c0
91582f9
46c1d5f
2c5ffad
d91418a
941b1af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
uuid: 8e17f221-14a1-4920-b33f-4739d8ddf543 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
config: | ||
- field.storage.node.field_cc_meta_tags | ||
- node.type.vba_facility | ||
module: | ||
- entity_field_fetch | ||
- tmgmt_content | ||
third_party_settings: | ||
tmgmt_content: | ||
excluded: false | ||
id: node.vba_facility.field_cc_meta_tags | ||
field_name: field_cc_meta_tags | ||
entity_type: node | ||
bundle: vba_facility | ||
label: 'Meta tags' | ||
description: '' | ||
required: false | ||
translatable: false | ||
default_value: { } | ||
default_value_callback: '' | ||
settings: | ||
target_entity_type: node | ||
target_entity_id: '61744' | ||
field_to_fetch: field_content_block | ||
target_paragraph_uuid: '142580' | ||
field_type: entity_field_fetch |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
uuid: 04efffc3-15ed-43b2-8079-dd55b0f7a388 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
module: | ||
- entity_field_fetch | ||
- node | ||
id: node.field_cc_meta_tags | ||
field_name: field_cc_meta_tags | ||
entity_type: node | ||
type: entity_field_fetch | ||
settings: { } | ||
module: entity_field_fetch | ||
locked: false | ||
cardinality: 1 | ||
translatable: true | ||
indexes: { } | ||
persist_with_no_fields: false | ||
custom_storage: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
uuid: 8dab1acd-96b7-4f47-adff-d38ea93ea39a | ||
langcode: en | ||
status: true | ||
dependencies: { } | ||
id: node__vba_facility | ||
label: 'Content: VBA Facility' | ||
tags: | ||
description: '[node:field_cc_meta_tags]' | ||
og_description: '[node:field_cc_meta_tags]' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Set VBA facility meta tags. | ||
* | ||
* VBA Facility needs to have meta tags populated | ||
* with data from an entity fetch field. | ||
* | ||
* VACMS-14000-vba-facility-meta-tags | ||
*/ | ||
|
||
require_once __DIR__ . '/script-library.php'; | ||
|
||
use Psr\Log\LogLevel; | ||
|
||
$sandbox = ['#finished' => 0]; | ||
do { | ||
print(va_gov_set_vba_facility_meta_tags($sandbox)); | ||
} while ($sandbox['#finished'] < 1); | ||
// Node processing complete. Call this done. | ||
return; | ||
|
||
/** | ||
* Setting the VBA facility meta tags. | ||
* | ||
* @param array $sandbox | ||
* Modeling the structure of hook_update_n sandbox. | ||
* | ||
* @return string | ||
* Status message. | ||
*/ | ||
function va_gov_set_vba_facility_meta_tags(array &$sandbox) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor and not worth moving. Consider for next time around that this could have been done as a "post deploy hook" so that it would run itself without needing a manual step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion. I'll look into that at the next opportunity. |
||
$node_storage = \Drupal::entityTypeManager()->getStorage('node'); | ||
|
||
// Get the count for VBA facilities. | ||
// This runs only once. | ||
if (!isset($sandbox['total'])) { | ||
$query = $node_storage->getQuery(); | ||
$query->condition('type', 'vba_facility'); | ||
$query->accessCheck(FALSE); | ||
$nids_to_update = $query->execute(); | ||
$result_count = count($nids_to_update); | ||
$sandbox['total'] = $result_count; | ||
$sandbox['current'] = 0; | ||
$sandbox['updated'] = 0; | ||
|
||
// Create non-numeric keys to accurately remove each nid when processed. | ||
$sandbox['nids_to_update'] = array_combine( | ||
array_map('_va_gov_stringifynid', array_values($nids_to_update)), | ||
array_values($nids_to_update) | ||
); | ||
} | ||
|
||
// Do not continue if no nodes are found. | ||
if (empty($sandbox['total'])) { | ||
$sandbox['#finished'] = 1; | ||
return "No VBA facility nodes found for processing.\n"; | ||
} | ||
|
||
$limit = 25; | ||
|
||
// Load entities. | ||
$node_ids = array_slice($sandbox['nids_to_update'], 0, $limit, TRUE); | ||
$nodes = $node_storage->loadMultiple($node_ids); | ||
foreach ($nodes as $node) { | ||
/** @var \Drupal\node\NodeInterface $node */ | ||
$nid = $node->id(); | ||
$nvid = $node->getRevisionId(); | ||
$node_storage = get_node_storage(); | ||
$latest_nvid = $node_storage->getLatestRevisionId($nid); | ||
|
||
update_field_cc_meta_tags_table($nid, $nvid); | ||
|
||
// Set the latest revision, if different than the default. | ||
if ($nvid < $latest_nvid) { | ||
update_field_cc_meta_tags_table($nid, $latest_nvid); | ||
} | ||
|
||
$sandbox['updated']++; | ||
$nids[] = $nid; | ||
|
||
unset($sandbox['nids_to_update']["node_{$nid}"]); | ||
$sandbox['current'] = $sandbox['total'] - count($sandbox['nids_to_update']); | ||
} | ||
|
||
// Log the processed nodes. | ||
Drupal::logger('va_gov_db') | ||
->log(LogLevel::INFO, 'VBA Facility update: Successfully updated %current nodes with meta tags from centralized content. Nodes updated: %nids', [ | ||
'%current' => $sandbox['current'], | ||
'%nids' => empty($nids) ? 'None' : implode(', ', $nids), | ||
]); | ||
|
||
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']); | ||
|
||
// Log the all-finished notice. | ||
if ($sandbox['#finished'] == 1) { | ||
Drupal::logger('va_gov_db')->log(LogLevel::INFO, 'Updating all %count VBA facility nodes with meta tags from centralized content.', [ | ||
'%count' => $sandbox['total'], | ||
]); | ||
return "VBA facility node updates complete. {$sandbox['current']} / {$sandbox['total']} - Total updated: {$sandbox['updated']}\n"; | ||
} | ||
|
||
return "Processed nodes... {$sandbox['current']} / {$sandbox['total']}.\n"; | ||
} | ||
|
||
/** | ||
* Update the node__field_cc_meta_tags table to set the entity fetch field. | ||
* | ||
* @param string $nodeId | ||
* The id of the node. | ||
* @param string $revisionId | ||
* The id of the node revision. | ||
*/ | ||
function update_field_cc_meta_tags_table(string $nodeId, string $revisionId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor and not worth changing. Consider abstracting this function one level deeper where it is not specific to meta tags but is generic to updating a fetch field. It would need to add a bundle parameter. Then move it to the script library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good thought. I was thinking that this is something that is going to likely need to happen again. |
||
$connection = \Drupal::database(); | ||
$connection->upsert('node__field_cc_meta_tags') | ||
->fields([ | ||
'bundle' => 'vba_facility', | ||
'entity_id' => $nodeId, | ||
'revision_id' => $revisionId, | ||
'langcode' => 'en', | ||
'delta' => 0, | ||
'field_cc_meta_tags_value' => 0, | ||
]) | ||
->key('revision_id') | ||
->execute(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems odd this is changing this now. This is unrelated to this PR isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is unrelated. I also thought it odd. But it's correct, and this vestigial workflow should be corrected from main. Are you suggesting we pull this change out and do it in a separate PR that is specific to workflow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Mainly just wanted to note it as odd... and thought maybe you could shed light on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is odd, so I ran a little test on Service Region. It turns out the dependency of the manage form display config is only picked up when updating the form itself. Changing the workflow does not result in a config change for the form. So I'll update the Service Region config, too, for consistency.