diff --git a/tide_core.install b/tide_core.install index 80afa1bd7..294ef7577 100644 --- a/tide_core.install +++ b/tide_core.install @@ -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; @@ -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(); + } +}