block_content.module 8.8 KB

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