102 lines
3.1 KiB
PHP
102 lines
3.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
*/
|
|
|
|
use Drupal\field\Entity\FieldConfig;
|
|
use Drupal\field\Entity\FieldStorageConfig;
|
|
use Drupal\Core\Database\Query\ConditionInterface;
|
|
|
|
/**
|
|
* Adds the default body field to a block type.
|
|
*
|
|
* @param string $block_type_id
|
|
* Id of the block type.
|
|
* @param string $label
|
|
* (optional) The label for the body instance. Defaults to 'Body'.
|
|
*
|
|
* @return \Drupal\field\Entity\FieldConfig
|
|
* A Body field object.
|
|
*/
|
|
function block_content_add_body_field($block_type_id, $label = 'Body') {
|
|
// Add or remove the body field, as needed.
|
|
$field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
|
|
if (empty($field)) {
|
|
$field = FieldConfig::create([
|
|
'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
|
|
'bundle' => $block_type_id,
|
|
'label' => $label,
|
|
'settings' => [
|
|
'display_summary' => FALSE,
|
|
'allowed_formats' => [],
|
|
],
|
|
]);
|
|
$field->save();
|
|
|
|
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
|
|
$display_repository = \Drupal::service('entity_display.repository');
|
|
|
|
// Assign widget settings for the default form mode.
|
|
$display_repository->getFormDisplay('block_content', $block_type_id)
|
|
->setComponent('body', [
|
|
'type' => 'text_textarea_with_summary',
|
|
])
|
|
->save();
|
|
|
|
// Assign display settings for default view mode.
|
|
$display_repository->getViewDisplay('block_content', $block_type_id)
|
|
->setComponent('body', [
|
|
'label' => 'hidden',
|
|
'type' => 'text_default',
|
|
])
|
|
->save();
|
|
}
|
|
|
|
return $field;
|
|
}
|
|
|
|
/**
|
|
* Utility function to find nested conditions using the reusable field.
|
|
*
|
|
* @todo Replace this function with a call to the API in
|
|
* https://www.drupal.org/project/drupal/issues/2984930
|
|
*
|
|
* @param array $condition
|
|
* The condition or condition group to check.
|
|
* @param array $tables
|
|
* The tables from the related select query.
|
|
*
|
|
* @see \Drupal\Core\Database\Query\SelectInterface::getTables
|
|
*
|
|
* @return bool
|
|
* Whether the conditions contain any condition using the reusable field.
|
|
*/
|
|
function _block_content_has_reusable_condition(array $condition, array $tables) {
|
|
// If this is a condition group call this function recursively for each nested
|
|
// condition until a condition is found that return TRUE.
|
|
if (isset($condition['#conjunction'])) {
|
|
foreach (array_filter($condition, 'is_array') as $nested_condition) {
|
|
if (_block_content_has_reusable_condition($nested_condition, $tables)) {
|
|
return TRUE;
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
if (isset($condition['field'])) {
|
|
$field = $condition['field'];
|
|
if (is_object($field) && $field instanceof ConditionInterface) {
|
|
return _block_content_has_reusable_condition($field->conditions(), $tables);
|
|
}
|
|
$field_parts = explode('.', $field);
|
|
$data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
|
|
foreach ($tables as $table) {
|
|
if ($table['table'] === $data_table && $field_parts[0] === $table['alias'] && $field_parts[1] === 'reusable') {
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
}
|
|
return FALSE;
|
|
}
|