This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentitygroupfield.module
106 lines (99 loc) · 3.48 KB
/
entitygroupfield.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @file
* Allows to add group content from entity form.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Implements hook_theme().
*/
function entitygroupfield_theme() {
return [
'entitygroupfield_dropbutton_wrapper' => [
'variables' => ['children' => NULL],
],
];
}
/**
* Implements hook_entity_base_field_info().
*/
function entitygroupfield_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_types = entitygroupfield_get_entity_types()) {
// Adding field to entity types.
if (array_key_exists($entity_type->id(), $entity_types)) {
$field_label = $entity_type->id() === 'user' ? t('Group memberships') : t('Groups');
$fields['entitygroupfield'] = BaseFieldDefinition::create('entitygroupfield')
->setName('entitygroupfield')
->setTargetEntityTypeId($entity_type->id())
->setSetting('target_type', 'group_content')
->setLabel($field_label)
->setTranslatable(FALSE)
->setComputed(TRUE)
->setCustomStorage(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setClass('\Drupal\entitygroupfield\Field\EntityGroupFieldItemList')
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'region' => 'hidden',
'weight' => 50,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'region' => 'hidden',
'weight' => 50,
]);
}
}
return $fields;
}
/**
* Implements hook_field_formatter_info_alter().
*/
function entitygroupfield_field_formatter_info_alter(array &$info) {
$info['entity_reference_entity_view']['field_types'][] = 'entitygroupfield';
}
/**
* Get entities that should have a group content field.
*/
function entitygroupfield_get_entity_types() {
$entity_types = [];
$plugin_manager = \Drupal::service('plugin.manager.group_content_enabler');
foreach ($plugin_manager->getDefinitions() as $plugin_definition) {
if ($entity_type_definition = \Drupal::entityTypeManager()->getDefinition($plugin_definition['entity_type_id'], FALSE)) {
// Check if fields can be attached to the entity type.
if ($entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
$entity_types[$plugin_definition['entity_type_id']]['label'] = (string) $entity_type_definition->getLabel();
$entity_types[$plugin_definition['entity_type_id']]['bundles'][] = $plugin_definition['entity_bundle'];
}
}
}
return $entity_types;
}
/**
* Returns the content enabler plugin IDs for a given entity type.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $bundle
* (optional) The entity bundle.
*
* @return string[]
* An array of group content enabler plugin IDs that match.
*/
function entitygroupfield_get_group_content_plugin_ids($entity_type_id, $bundle = NULL) {
$plugin_ids = [];
foreach (\Drupal::service('plugin.manager.group_content_enabler')->getDefinitions() as $plugin_id => $plugin_info) {
if ($plugin_info['entity_type_id'] === $entity_type_id) {
if (isset($bundle) && !empty($plugin_info['entity_bundle']) && $plugin_info['entity_bundle'] !== $bundle) {
continue;
}
$plugin_ids[] = $plugin_id;
}
}
return $plugin_ids;
}