Skip to content

Commit

Permalink
content category helpers for other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Nov 2, 2023
1 parent a8445aa commit bf853bf
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tide_core.install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
Expand Down Expand Up @@ -2176,3 +2177,49 @@ function _create_or_load_term($vocabulary_id, $term_name, $parent_tid = 0, $weig
// Return the new term.
return $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();
}
}

0 comments on commit bf853bf

Please sign in to comment.