-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththird_party_settings_example.module
More file actions
58 lines (50 loc) · 1.84 KB
/
third_party_settings_example.module
File metadata and controls
58 lines (50 loc) · 1.84 KB
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
<?php
/**
* @file
* The third party settings example module file.
*/
/**
* Implements hook_form_FORM_ID_alter().
*/
function third_party_settings_example_form_node_type_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$entity = $form_state->getFormObject()->getEntity();
if ($entity && $entity->id() === 'article') {
$exampleTextField = $entity->getThirdPartySetting('third_party_settings_example', 'text_field');
$form['example_settings'] = [
'#type' => 'fieldset',
'#tree' => FALSE,
'#title' => t('Third Party Example Settings'),
'#description' => t('For example purposes.'),
];
$form['example_settings']['text_field'] = [
'#type' => 'textfield',
'#title' => t('Example Text Field'),
'#default_value' => $exampleTextField,
];
$form['#entity_builders'][] = 'third_party_settings_example_node_form_builder';
}
}
/**
* Entity builder for the node:article entity.
*/
function third_party_settings_example_node_form_builder($entity_type, $entity, &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
if ($form_state->getValue('text_field')) {
$textFieldValue = $form_state->getValue('text_field');
$entity->setThirdPartySetting('third_party_settings_example', 'text_field', $textFieldValue);
return;
}
$entity->unsetThirdPartySetting('third_party_settings_example', 'text_field');
}
/**
* Implements hook_preprocess_HOOK().
*/
function third_party_settings_example_preprocess_node(&$variables) {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['elements']['#node'];
if ($node->bundle() == 'article') {
$additionalSetting = $node->type->entity->getThirdPartySetting('third_party_settings_example', 'text_field');
$variables['content']['additional_title'] = [
'#markup' => $additionalSetting,
];
}
}