generated from SU-SWS/stanford_module_example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstanford_fields.module
executable file
·106 lines (96 loc) · 3.71 KB
/
stanford_fields.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
* stanford_fields.module
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function stanford_fields_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\field\FieldConfigInterface $field_config */
$field_config = $form_state->get('field_config');
if ($field_config->getType() == 'link') {
$form['settings']['force_relative'] = [
'#type' => 'checkbox',
'#title' => t('Force relative internal links'),
'#default_value' => $field_config->getThirdPartySetting('stanford_fields', 'force_relative'),
'#states' => [
'invisible' => [
':input[name="settings[link_type]"]' => ['value' => '16'],
],
],
];
$form['#entity_builders'][] = 'stanford_fields_form_field_config_entity_builder';
}
}
/**
* Field config form submission to save the third party settings.
*
* @param string $entity_type
* Entity type ID.
* @param \Drupal\field\FieldConfigInterface $entity
* Submitted entity object.
* @param array $form
* Submitted form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Submitted form state.
*/
function stanford_fields_form_field_config_entity_builder(string $entity_type, FieldConfigInterface $entity, array &$form, FormStateInterface $form_state) {
if ($form_state->getValue(['settings', 'force_relative'])) {
$entity->setThirdPartySetting('stanford_fields', 'force_relative', TRUE);
return;
}
$entity->unsetThirdPartySetting('stanford_fields', 'force_relative');
}
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function stanford_fields_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
foreach ($fields as $field) {
if ($field->getType() == 'link' && $field->getThirdPartySetting('stanford_fields', 'force_relative')) {
$field->addConstraint('relative_internal_link', []);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Add validation to field add form.
*/
function stanford_fields_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#validate'][] = 'stanford_fields_field_storage_add_validate';
}
/**
* Field storage add validation to prevent hashed table names.
*
* @see \Drupal\Core\Entity\Sql\DefaultTableMapping::generateFieldTableName()
*/
function stanford_fields_field_storage_add_validate(&$form, FormStateInterface $form_state) {
$entity_type = $form_state->getBuildInfo()['args'][0];
$field_name = $form_state->getValue('field_name');
$field_prefix = \Drupal::config('field_ui.settings')->get('field_prefix');
// When a table name is over 48 characters, Drupal will hash the field name
// to create a shorter field table. This makes it nearly impossible to track
// down exactly what table is for what field without inspecting the field
// storage config entity.
if (strlen("{$entity_type}_revision__{$field_prefix}$field_name") > 48) {
$allowed_length = 48 - strlen("{$entity_type}_revision__{$field_prefix}");
$form_state->setError($form['new_storage_wrapper']['field_name'], t('Field name is too long. Please keep this field name under @count characters', ['@count' => $allowed_length]));
}
}
/**
* Implements hook_cron().
*/
function stanford_fields_cron() {
\Drupal::service('stanford_fields.field_cache')->invalidateDateFieldsCache();
}
/**
* Implements hook_preprocess_HOOK().
*/
function stanford_fields_preprocess_oembed_lazyload(&$variables) {
// Remove the ID attribute because it is not unique on the same page.
unset($variables['iframe']['#attributes']['id']);
}