-
Notifications
You must be signed in to change notification settings - Fork 0
/
yoast_seo.module
183 lines (162 loc) · 6.33 KB
/
yoast_seo.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @file
* yoast_seo.module
*/
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeTypeInterface;
/**
* Implements hook_form_node_type_form_alter().
*/
function yoast_seo_form_node_type_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var NodeTypeInterface $type */
$type = $form_state->getFormObject()->getEntity();
$field_manager = \Drupal::getContainer()->get('entity_field.manager');
$field_config = $field_manager->getFieldDefinitions('node', $type->get('type'));
$checkbox_disabled = TRUE;
$description = t('The Yoast SEO plugin requires a Metatag field.');
foreach ($field_config as $config) { /** @var BaseFieldDefinition $config */
if ($config->getType() == 'metatag') {
$checkbox_disabled = FALSE;
$description = t('Enable the Yoast SEO plugin for this content type');
}
}
// Add our settings as a separate tab
$form['yoast_seo'] = [
'#type' => 'details',
'#title' => t('Yoast SEO settings'),
'#group' => 'additional_settings',
'#weight' => 20
];
$form['yoast_seo']['yoast_enable'] = [
'#type' => 'checkbox',
'#title' => t('Enable'),
'#description' => $description,
'#default_value' => $type->getThirdPartySetting('yoast_seo', 'enabled', FALSE),
'#disabled' => $checkbox_disabled
];
// Save our settings onto the entity
$form['#entity_builders'][] = 'yoast_seo_form_node_type_form_builder';
}
/**
* Entity builder for the node type form with Yoast SEO options.
*
* @param $entity_type
* @param \Drupal\node\NodeTypeInterface $type
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function yoast_seo_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
$type->setThirdPartySetting('yoast_seo', 'enabled', $form_state->getValue('yoast_enable', FALSE));
// Look for an instance of the field
/** @var FieldConfig $instance */
$instance = \Drupal\field\Entity\FieldConfig::loadByName('node', $type->get('type'), 'field_node_yoast_seo');
// Load the entity form display
$display_id = sprintf('node.%s.default', $type->get('type'));
/** @var EntityFormDisplay $form_display */
$form_display = \Drupal\Core\Entity\Entity\EntityFormDisplay::load($display_id);
if ($type->getThirdPartySetting('yoast_seo', 'enabled')) {
// Load or create the field storage configuration
$field = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_node_yoast_seo');
if (!$field) {
$field = \Drupal\field\Entity\FieldStorageConfig::create([
'type' => 'yoast_seo',
'field_name' => 'field_node_yoast_seo',
'weight' => 20,
'cardinality' => 1,
'entity_type' => 'node'
]);
$field->save();
}
// Attach an instance to the content type
if (!$instance) {
$instance = \Drupal\field\Entity\FieldConfig::create([
'entity_type' => 'node',
'bundle' => $type->get('type'),
'field_name' => 'field_node_yoast_seo',
'label' => t('Yoast SEO')
]);
$instance->save();
}
if (!$form_display->getComponent('field_node_yoast_seo')) {
$form_display->setComponent('field_node_yoast_seo', [
'type' => 'yoast_seo_default',
'weight' => 20
]);
$form_display->save();
}
}
elseif ($instance) {
// Remove the instance if the Yoast SEO support has been disabled
$instance->delete();
// Delete the component from the edit form
if ($form_display->getComponent('field_node_yoast_seo')) {
$form_display
->removeComponent('field_node_yoast_seo')
->save();
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function yoast_seo_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
/** @var FieldConfig $field_config */
$field_config = $form_state->getFormObject()->getEntity();
if ($field_config->getType() == 'yoast_seo') {
drupal_set_message(t('You\'re not supposed to edit these settings, so I disabled them for you.'), 'warning');
$form['label']['#disabled'] = TRUE;
$form['description']['#disabled'] = TRUE;
$form['required']['#disabled'] = TRUE;
$form['default_value']['#access'] = FALSE;
$form['actions']['#access'] = FALSE;
}
}
/**
* Implements hook_form_field_storage_config_edit_form_alter().
*/
function yoast_seo_form_field_storage_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
if ($form_state->getFormObject()->getEntity()->getType() == 'yoast_seo') {
// Disble the cardinality option on the field storage settings form
$form['cardinality_container']['#prefix'] = t('Yoast SEO fields must be singular');
$form['cardinality_container']['#disabled'] = TRUE;
$form['cardinality_container']['cardinality']['#disabled'] = TRUE;
$form['cardinality_container']['cardinality_number']['#disabled'] = TRUE;
unset($form['cardinality_container']['cardinality_number']['#states']);
}
}
/**
* Implements hook_form_node_form_alter().
*/
function yoast_seo_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
/** @var EntityInterface $node */
$node = $form_state->getFormObject()->getEntity();
/** @var NodeType $type */
$type = NodeType::load($node->bundle());
if ($type->getThirdPartySetting('yoast_seo', 'enabled')) {
// Attach our custom Javascript, but we need the rendered ids for that
$form['#after_build'][] = 'yoast_seo_node_form_after_build';
}
}
/**
* 'after_build' action of the node_form that will attach our custom Javascript
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @return mixed
*/
function yoast_seo_node_form_after_build($form, FormStateInterface &$form_state) {
/** @var Node $node */
$node = $form_state->getFormObject()->getEntity();
$yoast_service = \Drupal::getContainer()->get('yoast.config_service');
$form['#attached']['drupalSettings']['yoast_seo'] = $yoast_service->getConfigration($form, $node);
$form['#attached']['library'][] = 'yoast_seo/yoast_seo';
$form['#attached']['library'][] = 'yoast_seo/init';
return $form;
}