Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Allows the creation of custom blocks through the user interface.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
10 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
11 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@17
|
12 use Drupal\Core\Database\Query\SelectInterface;
|
Chris@17
|
13 use Drupal\Core\Database\Query\AlterableInterface;
|
Chris@17
|
14 use Drupal\Core\Database\Query\ConditionInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Implements hook_help().
|
Chris@0
|
18 */
|
Chris@0
|
19 function block_content_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
20 switch ($route_name) {
|
Chris@0
|
21 case 'help.page.block_content':
|
Chris@18
|
22 $field_ui = \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#';
|
Chris@0
|
23 $output = '';
|
Chris@0
|
24 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@18
|
25 $output .= '<p>' . t('The Custom Block module allows you to create and manage custom <em>block types</em> and <em>content-containing blocks</em> from the <a href=":block-library">Custom block library</a> page. Custom block types have fields; see the <a href=":field-help">Field module help</a> for more information. Once created, custom blocks can be placed in regions just like blocks provided by other modules; see the <a href=":blocks">Block module help</a> page for details. For more information, see the <a href=":online-help">online documentation for the Custom Block module</a>.', [':block-library' => Url::fromRoute('entity.block_content.collection')->toString(), ':block-content' => Url::fromRoute('entity.block_content.collection')->toString(), ':field-help' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':blocks' => Url::fromRoute('help.page', ['name' => 'block'])->toString(), ':online-help' => 'https://www.drupal.org/documentation/modules/block_content']) . '</p>';
|
Chris@0
|
26 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
27 $output .= '<dl>';
|
Chris@0
|
28 $output .= '<dt>' . t('Creating and managing custom block types') . '</dt>';
|
Chris@18
|
29 $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create and edit custom block types with fields and display settings, from the <a href=":types">Block types</a> page in the Custom block library. For more information about managing fields and display settings, see the <a href=":field-ui">Field UI module help</a>.', [':types' => Url::fromRoute('entity.block_content_type.collection')->toString(), ':field-ui' => $field_ui]) . '</dd>';
|
Chris@0
|
30 $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
|
Chris@18
|
31 $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create, edit, and delete custom blocks of each defined custom block type, from the <a href=":block-library">Blocks</a> page in the Custom block library. After creating a block, place it in a region from the <a href=":blocks">Block layout</a> page; see the <a href=":block_help">Block module help</a> for more information about placing blocks.', [':blocks' => Url::fromRoute('block.admin_display')->toString(), ':block-library' => Url::fromRoute('entity.block_content.collection')->toString(), ':block_help' => Url::fromRoute('help.page', ['name' => 'block'])->toString()]) . '</dd>';
|
Chris@0
|
32 $output .= '</dl>';
|
Chris@0
|
33 return $output;
|
Chris@0
|
34
|
Chris@0
|
35 case 'entity.block_content.collection':
|
Chris@18
|
36 $output = '<p>' . t('Blocks in the block library belong to <a href=":types">Custom block types</a>, each with its own fields and display settings. After creating a block, place it in a region from the <a href=":blocks">Block layout</a> page.', [':types' => Url::fromRoute('entity.block_content_type.collection')->toString(), ':blocks' => Url::fromRoute('block.admin_display')->toString()]) . '</p>';
|
Chris@0
|
37 return $output;
|
Chris@0
|
38
|
Chris@0
|
39 case 'entity.block_content_type.collection':
|
Chris@18
|
40 $output = '<p>' . t('Each block type has its own fields and display settings. Create blocks of each type on the <a href=":block-library">Blocks</a> page in the custom block library.', [':block-library' => Url::fromRoute('entity.block_content.collection')->toString()]) . '</p>';
|
Chris@0
|
41 return $output;
|
Chris@0
|
42
|
Chris@0
|
43 }
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Implements hook_theme().
|
Chris@0
|
48 */
|
Chris@0
|
49 function block_content_theme($existing, $type, $theme, $path) {
|
Chris@0
|
50 return [
|
Chris@0
|
51 'block_content_add_list' => [
|
Chris@0
|
52 'variables' => ['content' => NULL],
|
Chris@0
|
53 'file' => 'block_content.pages.inc',
|
Chris@0
|
54 ],
|
Chris@0
|
55 ];
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Implements hook_entity_type_alter().
|
Chris@0
|
60 */
|
Chris@0
|
61 function block_content_entity_type_alter(array &$entity_types) {
|
Chris@0
|
62 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
|
Chris@0
|
63 // Add a translation handler for fields if the language module is enabled.
|
Chris@0
|
64 if (\Drupal::moduleHandler()->moduleExists('language')) {
|
Chris@0
|
65 $translation = $entity_types['block_content']->get('translation');
|
Chris@0
|
66 $translation['block_content'] = TRUE;
|
Chris@0
|
67 $entity_types['block_content']->set('translation', $translation);
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Adds the default body field to a custom block type.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param string $block_type_id
|
Chris@0
|
75 * Id of the block type.
|
Chris@0
|
76 * @param string $label
|
Chris@0
|
77 * (optional) The label for the body instance. Defaults to 'Body'
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return \Drupal\field\Entity\FieldConfig
|
Chris@0
|
80 * A Body field object.
|
Chris@0
|
81 */
|
Chris@0
|
82 function block_content_add_body_field($block_type_id, $label = 'Body') {
|
Chris@0
|
83 // Add or remove the body field, as needed.
|
Chris@0
|
84 $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
|
Chris@0
|
85 if (empty($field)) {
|
Chris@0
|
86 $field = FieldConfig::create([
|
Chris@0
|
87 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
|
Chris@0
|
88 'bundle' => $block_type_id,
|
Chris@0
|
89 'label' => $label,
|
Chris@0
|
90 'settings' => ['display_summary' => FALSE],
|
Chris@0
|
91 ]);
|
Chris@0
|
92 $field->save();
|
Chris@0
|
93
|
Chris@0
|
94 // Assign widget settings for the 'default' form mode.
|
Chris@0
|
95 entity_get_form_display('block_content', $block_type_id, 'default')
|
Chris@0
|
96 ->setComponent('body', [
|
Chris@0
|
97 'type' => 'text_textarea_with_summary',
|
Chris@0
|
98 ])
|
Chris@0
|
99 ->save();
|
Chris@0
|
100
|
Chris@0
|
101 // Assign display settings for 'default' view mode.
|
Chris@0
|
102 entity_get_display('block_content', $block_type_id, 'default')
|
Chris@0
|
103 ->setComponent('body', [
|
Chris@0
|
104 'label' => 'hidden',
|
Chris@0
|
105 'type' => 'text_default',
|
Chris@0
|
106 ])
|
Chris@0
|
107 ->save();
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 return $field;
|
Chris@0
|
111 }
|
Chris@17
|
112
|
Chris@17
|
113 /**
|
Chris@17
|
114 * Implements hook_query_TAG_alter().
|
Chris@17
|
115 *
|
Chris@17
|
116 * Alters any 'entity_reference' query where the entity type is
|
Chris@17
|
117 * 'block_content' and the query has the tag 'block_content_access'.
|
Chris@17
|
118 *
|
Chris@17
|
119 * These queries should only return reusable blocks unless a condition on
|
Chris@17
|
120 * 'reusable' is explicitly set.
|
Chris@17
|
121 *
|
Chris@17
|
122 * Block_content entities that are reusable should by default not be selectable
|
Chris@17
|
123 * as entity reference values. A module can still create an instance of
|
Chris@17
|
124 * \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
|
Chris@17
|
125 * that will allow selection of non-reusable blocks by explicitly setting
|
Chris@17
|
126 * a condition on the 'reusable' field.
|
Chris@17
|
127 *
|
Chris@17
|
128 * @see \Drupal\block_content\BlockContentAccessControlHandler
|
Chris@17
|
129 */
|
Chris@17
|
130 function block_content_query_entity_reference_alter(AlterableInterface $query) {
|
Chris@17
|
131 if ($query instanceof SelectInterface && $query->getMetaData('entity_type') === 'block_content' && $query->hasTag('block_content_access')) {
|
Chris@17
|
132 $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
|
Chris@17
|
133 if (array_key_exists($data_table, $query->getTables()) && !_block_content_has_reusable_condition($query->conditions(), $query->getTables())) {
|
Chris@17
|
134 $query->condition("$data_table.reusable", TRUE);
|
Chris@17
|
135 }
|
Chris@17
|
136 }
|
Chris@17
|
137 }
|
Chris@17
|
138
|
Chris@17
|
139 /**
|
Chris@17
|
140 * Utility function to find nested conditions using the reusable field.
|
Chris@17
|
141 *
|
Chris@17
|
142 * @todo Replace this function with a call to the API in
|
Chris@17
|
143 * https://www.drupal.org/project/drupal/issues/2984930
|
Chris@17
|
144 *
|
Chris@17
|
145 * @param array $condition
|
Chris@17
|
146 * The condition or condition group to check.
|
Chris@17
|
147 * @param array $tables
|
Chris@17
|
148 * The tables from the related select query.
|
Chris@17
|
149 *
|
Chris@17
|
150 * @see \Drupal\Core\Database\Query\SelectInterface::getTables
|
Chris@17
|
151 *
|
Chris@17
|
152 * @return bool
|
Chris@17
|
153 * Whether the conditions contain any condition using the reusable field.
|
Chris@17
|
154 */
|
Chris@17
|
155 function _block_content_has_reusable_condition(array $condition, array $tables) {
|
Chris@17
|
156 // If this is a condition group call this function recursively for each nested
|
Chris@17
|
157 // condition until a condition is found that return TRUE.
|
Chris@17
|
158 if (isset($condition['#conjunction'])) {
|
Chris@17
|
159 foreach (array_filter($condition, 'is_array') as $nested_condition) {
|
Chris@17
|
160 if (_block_content_has_reusable_condition($nested_condition, $tables)) {
|
Chris@17
|
161 return TRUE;
|
Chris@17
|
162 }
|
Chris@17
|
163 }
|
Chris@17
|
164 return FALSE;
|
Chris@17
|
165 }
|
Chris@17
|
166 if (isset($condition['field'])) {
|
Chris@17
|
167 $field = $condition['field'];
|
Chris@17
|
168 if (is_object($field) && $field instanceof ConditionInterface) {
|
Chris@17
|
169 return _block_content_has_reusable_condition($field->conditions(), $tables);
|
Chris@17
|
170 }
|
Chris@17
|
171 $field_parts = explode('.', $field);
|
Chris@17
|
172 $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
|
Chris@17
|
173 foreach ($tables as $table) {
|
Chris@17
|
174 if ($table['table'] === $data_table && $field_parts[0] === $table['alias'] && $field_parts[1] === 'reusable') {
|
Chris@17
|
175 return TRUE;
|
Chris@17
|
176 }
|
Chris@17
|
177 }
|
Chris@17
|
178
|
Chris@17
|
179 }
|
Chris@17
|
180 return FALSE;
|
Chris@17
|
181 }
|