-
Notifications
You must be signed in to change notification settings - Fork 0
/
emr.module
73 lines (62 loc) · 2.19 KB
/
emr.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
<?php
/**
* @file
* Entity Meta Relation module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\emr\ContentFormHandlerInterface;
/**
* Implements hook_form_alter().
*/
function emr_form_alter(&$form, FormStateInterface &$form_state) {
$form_object = $form_state->getFormObject();
if (!$form_object instanceof ContentEntityForm ||
!in_array($form_object->getOperation(), ['add', 'edit', 'default'])) {
return;
}
$entity = $form_object->getEntity();
if (!\Drupal::entityTypeManager()->hasHandler($entity->getEntityTypeId(), 'emr_content_form')) {
return;
}
// We don't want to show the meta form on the translation form of the entity.
if (!$entity->isDefaultTranslation()) {
return;
}
$handler = \Drupal::entityTypeManager()->getHandler($entity->getEntityTypeId(), 'emr_content_form');
if ($handler instanceof ContentFormHandlerInterface) {
$form = $handler->addFormElements($form, $form_state, $entity);
}
}
/**
* Implements hook_inline_entity_form_reference_form_alter().
*/
function emr_inline_entity_form_entity_form_alter(&$reference_form, &$form_state) {
// Hide non desirable fields for entity meta when show in inline entity form.
if ($reference_form['#entity_type'] == 'entity_meta') {
$reference_form['status']['#access'] = FALSE;
$reference_form['created']['#access'] = FALSE;
$reference_form['revision_log']['#access'] = FALSE;
}
}
/**
* Implements hook_entity_base_field_info().
*/
function emr_entity_base_field_info(EntityTypeInterface $entity_type) {
if (!$entity_type->get('entity_meta_relation_bundle')) {
// If the entity type is not connected with an EntityMeta, we don't need to
// do anything.
return [];
}
// Add emr computed field to have the related entity metas.
$fields['emr_entity_metas'] = BaseFieldDefinition::create('emr_item_entity_metas')
->setName('emr_entity_metas')
->setLabel(t('Entity metas'))
->setComputed(TRUE)
->setSetting('target_type', 'entity_meta')
->setDisplayConfigurable('view', FALSE);
return $fields;
}