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