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