forked from dpc-sdp/tide_demo_content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtide_demo_content.module
84 lines (78 loc) · 3.13 KB
/
tide_demo_content.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* @file
* Tide Demo Content.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\tide_site\TideSiteFields;
use Drupal\yaml_content\ContentLoader\ContentLoaderInterface;
/**
* Implements hook_tide_demo_content_entity_imported().
*/
function tide_demo_content_tide_demo_content_entity_imported(EntityInterface $entity, $content_data, ContentLoaderInterface $content_loader) {
$entity_type = $entity->getEntityTypeId();
// Assign an existing Site to imported entities.
if (\Drupal::moduleHandler()->moduleExists('tide_site')) {
/** @var \Drupal\tide_site\TideSiteHelper $site_helper */
$site_helper = \Drupal::service('tide_site.helper');
if (!$site_helper->isSupportedEntityType($entity_type)) {
return;
}
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity_sites = $site_helper->getEntitySites($entity);
$sites = $site_helper->getAllSites();
if (!empty($sites)) {
// Attempt to find the very first Site with the smallest tid.
ksort($sites, SORT_ASC);
try {
foreach ($sites as $site_id => $site) {
$trail = $site_helper->getSiteTrail($site_id);
// Site term only has 1 item in its trail - choose this Site.
if (count($trail) == 1) {
$field_site_field_name = TideSiteFields::normaliseFieldName(TideSiteFields::FIELD_SITE, $entity_type);
if ($entity->hasField($field_site_field_name)) {
// This Site hasn't been assigned to the entity.
if (!isset($entity_sites['ids'][$site_id])) {
$entity->{$field_site_field_name}[] = ['target_id' => $site_id];
}
}
$field_primary_site_field_name = TideSiteFields::normaliseFieldName(TideSiteFields::FIELD_PRIMARY_SITE, $entity_type);
// Update the Primary Site field if needed.
if ($entity->hasField($field_primary_site_field_name) && $entity->get($field_primary_site_field_name)->isEmpty()) {
$entity->$field_primary_site_field_name->target_id = $site_id;
}
// Should not create a new revision.
if ($entity instanceof RevisionableInterface) {
$entity->setNewRevision(FALSE);
}
$entity->save();
}
}
}
catch (\Exception $exception) {
\Drupal::messenger()->addError($exception->getMessage());
watchdog_exception('tide_demo_content', $exception);
}
}
}
}
/**
* Implements hook_entity_delete().
*/
function tide_demo_content_entity_delete(EntityInterface $entity) {
// Removed the deleted entity from tracked demo entities.
/** @var \Drupal\tide_demo_content\DemoContentRepository $repository */
$repository = \Drupal::service('tide_demo_content.repository');
$repository->untrackEntity($entity);
}
/**
* Implements hook_config_ignore_settings_alter().
*/
function tide_demo_content_config_ignore_settings_alter(array &$settings) {
// Ignore all demo configs.
$settings[] = '*tide_demo*';
$settings[] = '*tide-demo*';
$settings[] = '*tide_demo_content*';
$settings[] = '*tide-demo-content*';
}