block_content.module 8.4 KB

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