Chris@0: moduleExists('field_ui') ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#'; Chris@0: $output = ''; Chris@0: $output .= '
' . t('The Custom Block module allows you to create and manage custom block types and content-containing blocks from the Custom block library page. Custom block types have fields; see the Field module help for more information. Once created, custom blocks can be placed in regions just like blocks provided by other modules; see the Block module help page for details. For more information, see the online documentation for the Custom Block module.', [':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']) . '
'; Chris@0: $output .= '' . t('Blocks in the block library belong to Custom block types, each with its own fields and display settings. After creating a block, place it in a region from the Block layout page.', [':types' => Url::fromRoute('entity.block_content_type.collection')->toString(), ':blocks' => Url::fromRoute('block.admin_display')->toString()]) . '
'; Chris@0: return $output; Chris@0: Chris@0: case 'entity.block_content_type.collection': Chris@5: $output = '' . t('Each block type has its own fields and display settings. Create blocks of each type on the Blocks page in the custom block library.', [':block-library' => Url::fromRoute('entity.block_content.collection')->toString()]) . '
'; Chris@0: return $output; Chris@0: Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme(). Chris@0: */ Chris@0: function block_content_theme($existing, $type, $theme, $path) { Chris@0: return [ Chris@0: 'block_content_add_list' => [ Chris@0: 'variables' => ['content' => NULL], Chris@0: 'file' => 'block_content.pages.inc', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_entity_type_alter(). Chris@0: */ Chris@0: function block_content_entity_type_alter(array &$entity_types) { Chris@0: /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ Chris@0: // Add a translation handler for fields if the language module is enabled. Chris@0: if (\Drupal::moduleHandler()->moduleExists('language')) { Chris@0: $translation = $entity_types['block_content']->get('translation'); Chris@0: $translation['block_content'] = TRUE; Chris@0: $entity_types['block_content']->set('translation', $translation); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds the default body field to a custom block type. Chris@0: * Chris@0: * @param string $block_type_id Chris@0: * Id of the block type. Chris@0: * @param string $label Chris@0: * (optional) The label for the body instance. Defaults to 'Body' Chris@0: * Chris@0: * @return \Drupal\field\Entity\FieldConfig Chris@0: * A Body field object. Chris@0: */ Chris@0: function block_content_add_body_field($block_type_id, $label = 'Body') { Chris@0: // Add or remove the body field, as needed. Chris@0: $field = FieldConfig::loadByName('block_content', $block_type_id, 'body'); Chris@0: if (empty($field)) { Chris@0: $field = FieldConfig::create([ Chris@0: 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'), Chris@0: 'bundle' => $block_type_id, Chris@0: 'label' => $label, Chris@0: 'settings' => ['display_summary' => FALSE], Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: // Assign widget settings for the 'default' form mode. Chris@0: entity_get_form_display('block_content', $block_type_id, 'default') Chris@0: ->setComponent('body', [ Chris@0: 'type' => 'text_textarea_with_summary', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Assign display settings for 'default' view mode. Chris@0: entity_get_display('block_content', $block_type_id, 'default') Chris@0: ->setComponent('body', [ Chris@0: 'label' => 'hidden', Chris@0: 'type' => 'text_default', Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: return $field; Chris@0: } Chris@4: Chris@4: /** Chris@4: * Implements hook_query_TAG_alter(). Chris@4: * Chris@4: * Alters any 'entity_reference' query where the entity type is Chris@4: * 'block_content' and the query has the tag 'block_content_access'. Chris@4: * Chris@4: * These queries should only return reusable blocks unless a condition on Chris@4: * 'reusable' is explicitly set. Chris@4: * Chris@4: * Block_content entities that are reusable should by default not be selectable Chris@4: * as entity reference values. A module can still create an instance of Chris@4: * \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface Chris@4: * that will allow selection of non-reusable blocks by explicitly setting Chris@4: * a condition on the 'reusable' field. Chris@4: * Chris@4: * @see \Drupal\block_content\BlockContentAccessControlHandler Chris@4: */ Chris@4: function block_content_query_entity_reference_alter(AlterableInterface $query) { Chris@4: if ($query instanceof SelectInterface && $query->getMetaData('entity_type') === 'block_content' && $query->hasTag('block_content_access')) { Chris@4: $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable(); Chris@4: if (array_key_exists($data_table, $query->getTables()) && !_block_content_has_reusable_condition($query->conditions(), $query->getTables())) { Chris@4: $query->condition("$data_table.reusable", TRUE); Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: /** Chris@4: * Utility function to find nested conditions using the reusable field. Chris@4: * Chris@4: * @todo Replace this function with a call to the API in Chris@4: * https://www.drupal.org/project/drupal/issues/2984930 Chris@4: * Chris@4: * @param array $condition Chris@4: * The condition or condition group to check. Chris@4: * @param array $tables Chris@4: * The tables from the related select query. Chris@4: * Chris@4: * @see \Drupal\Core\Database\Query\SelectInterface::getTables Chris@4: * Chris@4: * @return bool Chris@4: * Whether the conditions contain any condition using the reusable field. Chris@4: */ Chris@4: function _block_content_has_reusable_condition(array $condition, array $tables) { Chris@4: // If this is a condition group call this function recursively for each nested Chris@4: // condition until a condition is found that return TRUE. Chris@4: if (isset($condition['#conjunction'])) { Chris@4: foreach (array_filter($condition, 'is_array') as $nested_condition) { Chris@4: if (_block_content_has_reusable_condition($nested_condition, $tables)) { Chris@4: return TRUE; Chris@4: } Chris@4: } Chris@4: return FALSE; Chris@4: } Chris@4: if (isset($condition['field'])) { Chris@4: $field = $condition['field']; Chris@4: if (is_object($field) && $field instanceof ConditionInterface) { Chris@4: return _block_content_has_reusable_condition($field->conditions(), $tables); Chris@4: } Chris@4: $field_parts = explode('.', $field); Chris@4: $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable(); Chris@4: foreach ($tables as $table) { Chris@4: if ($table['table'] === $data_table && $field_parts[0] === $table['alias'] && $field_parts[1] === 'reusable') { Chris@4: return TRUE; Chris@4: } Chris@4: } Chris@4: Chris@4: } Chris@4: return FALSE; Chris@4: }