Initial Drupal 11 with DDEV setup

This commit is contained in:
gluebox
2025-10-08 11:39:17 -04:00
commit 89ef74b305
25344 changed files with 2599172 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?php
namespace Drupal\text\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Hook implementations for text.
*/
class TextHooks {
use StringTranslationTrait;
/**
* Implements hook_help().
*/
#[Hook('help')]
public function help($route_name, RouteMatchInterface $route_match): ?string {
switch ($route_name) {
case 'help.page.text':
$output = '';
$output .= '<h2>' . $this->t('About') . '</h2>';
$output .= '<p>' . $this->t('The Text module allows you to create short and long text fields with optional summaries. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":text_documentation">online documentation for the Text module</a>.', [
':field' => Url::fromRoute('help.page', [
'name' => 'field',
])->toString(),
':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
'name' => 'field_ui',
])->toString() : '#',
':text_documentation' => 'https://www.drupal.org/documentation/modules/text',
]) . '</p>';
$output .= '<h2>' . $this->t('Uses') . '</h2>';
$output .= '<dl>';
$output .= '<dt>' . $this->t('Managing and displaying text fields') . '</dt>';
$output .= '<dd>' . $this->t('The <em>settings</em> and <em>display</em> of the text field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [
':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
'name' => 'field_ui',
])->toString() : '#',
]) . '</dd>';
$output .= '<dt>' . $this->t('Creating short text fields') . '</dt>';
$output .= '<dd>' . $this->t('If you choose <em>Text (plain)</em> or <em>Text (formatted)</em> as the field type on the <em>Manage fields</em> page, then a field with a single row is displayed. You can change the maximum text length in the <em>Field settings</em> when you set up the field.') . '</dd>';
$output .= '<dt>' . $this->t('Creating long text fields') . '</dt>';
$output .= '<dd>' . $this->t('If you choose <em>Text (plain, long)</em>, <em>Text (formatted, long)</em>, or <em>Text (formatted, long, with summary)</em> on the <em>Manage fields</em> page, then users can insert text of unlimited length. On the <em>Manage form display</em> page, you can set the number of rows that are displayed to users.') . '</dd>';
$output .= '<dt>' . $this->t('Trimming the text length') . '</dt>';
$output .= '<dd>' . $this->t('On the <em>Manage display</em> page you can choose to display a trimmed version of the text, and if so, where to cut off the text.') . '</dd>';
$output .= '<dt>' . $this->t('Displaying summaries instead of trimmed text') . '</dt>';
$output .= '<dd>' . $this->t('As an alternative to using a trimmed version of the text, you can enter a separate summary by choosing the <em>Text (formatted, long, with summary)</em> field type on the <em>Manage fields</em> page. Even when <em>Summary input</em> is enabled, and summaries are provided, you can display <em>trimmed</em> text nonetheless by choosing the appropriate format on the <em>Manage display</em> page.') . '</dd>';
$output .= '<dt>' . $this->t('Using text formats and editors') . '</dt>';
$output .= '<dd>' . $this->t('If you choose <em>Text (plain)</em> or <em>Text (plain, long)</em> you restrict the input to <em>Plain text</em> only. If you choose <em>Text (formatted)</em>, <em>Text (formatted, long)</em>, or <em>Text (formatted, long with summary)</em> you allow users to write formatted text. Which options are available to individual users depends on the settings on the <a href=":formats">Text formats and editors page</a>.', [':formats' => Url::fromRoute('filter.admin_overview')->toString()]) . '</dd>';
$output .= '</dl>';
return $output;
}
return NULL;
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Drupal\text\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'text_default' formatter.
*/
#[FieldFormatter(
id: 'text_default',
label: new TranslatableMarkup('Default'),
field_types: [
'text',
'text_long',
'text_with_summary',
],
)]
class TextDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
// The ProcessedText element already handles cache context & tag bubbling.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item->getLangcode(),
];
}
return $elements;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Drupal\text\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'text_summary_or_trimmed' formatter.
*/
#[FieldFormatter(
id: 'text_summary_or_trimmed',
label: new TranslatableMarkup('Summary or trimmed'),
field_types: [
'text_with_summary',
],
)]
class TextSummaryOrTrimmedFormatter extends TextTrimmedFormatter {}

View File

@ -0,0 +1,133 @@
<?php
namespace Drupal\text\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'text_trimmed' formatter.
*
* Note: This class also contains the implementations used by the
* 'text_summary_or_trimmed' formatter.
*
* @see \Drupal\text\Field\Formatter\TextSummaryOrTrimmedFormatter
*/
#[FieldFormatter(
id: 'text_trimmed',
label: new TranslatableMarkup('Trimmed'),
field_types: [
'text',
'text_long',
'text_with_summary',
],
)]
class TextTrimmedFormatter extends FormatterBase implements TrustedCallbackInterface {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'trim_length' => '600',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['trim_length'] = [
'#title' => $this->t('Trimmed limit'),
'#type' => 'number',
'#field_suffix' => $this->t('characters'),
'#default_value' => $this->getSetting('trim_length'),
'#description' => $this->t('If the summary is not set, the trimmed %label field will end at the last full sentence before this character limit.', ['%label' => $this->fieldDefinition->getLabel()]),
'#min' => 1,
'#required' => TRUE,
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Trimmed limit: @trim_length characters', ['@trim_length' => $this->getSetting('trim_length')]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$render_as_summary = function (&$element) {
// Make sure any default #pre_render callbacks are set on the element,
// because text_pre_render_summary() must run last.
$element += \Drupal::service('element_info')->getInfo($element['#type']);
// Add the #pre_render callback that renders the text into a summary.
$element['#pre_render'][] = [TextTrimmedFormatter::class, 'preRenderSummary'];
// Pass on the trim length to the #pre_render callback via a property.
$element['#text_summary_trim_length'] = $this->getSetting('trim_length');
};
// The ProcessedText element already handles cache context & tag bubbling.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'processed_text',
'#text' => NULL,
'#format' => $item->format,
'#langcode' => $item->getLangcode(),
];
if ($this->getPluginId() == 'text_summary_or_trimmed' && !empty($item->summary)) {
$elements[$delta]['#text'] = $item->summary;
}
else {
$elements[$delta]['#text'] = $item->value;
$render_as_summary($elements[$delta]);
}
}
return $elements;
}
/**
* Pre-render callback: Renders a processed text element's #markup summary.
*
* @param array $element
* A structured array with the following key-value pairs:
* - #markup: the filtered text (as filtered by filter_pre_render_text())
* - #format: containing the machine name of the filter format to be used to
* filter the text. Defaults to the fallback format. See
* filter_fallback_format().
* - #text_summary_trim_length: the desired character length of the summary
* (used by text_summary())
*
* @return array
* The passed-in element with the filtered text in '#markup' trimmed.
*
* @see filter_pre_render_text()
* @see text_summary()
*/
public static function preRenderSummary(array $element) {
$element['#markup'] = text_summary($element['#markup'], $element['#format'], $element['#text_summary_trim_length']);
return $element;
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['preRenderSummary'];
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Drupal\text\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines an item list class for text fields.
*/
class TextFieldItemList extends FieldItemList {
/**
* {@inheritdoc}
*/
public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state) {
if ($allowed_formats = $this->getSetting('allowed_formats')) {
$field_name = $this->definition->getName();
$submitted_values = $form_state->getValue([
'default_value_input',
$field_name,
]);
foreach ($submitted_values as $delta => $value) {
if (!in_array($value['format'], $allowed_formats, TRUE)) {
$form_state->setErrorByName(
"default_value_input][{$field_name}][{$delta}][format",
$this->t("The selected text format is not allowed.")
);
}
}
}
parent::defaultValuesFormValidate($element, $form, $form_state);
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace Drupal\text\Plugin\Field\FieldType;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'text' field type.
*/
#[FieldType(
id: "text",
label: new TranslatableMarkup("Text (formatted)"),
description: [
new TranslatableMarkup("Ideal for titles and names that need to support markup such as bold, italics or links"),
new TranslatableMarkup("Efficient storage for short text"),
new TranslatableMarkup("Requires specifying a maximum length"),
new TranslatableMarkup("Good for fields with known or predictable lengths"),
],
category: "formatted_text",
default_widget: "text_textfield",
default_formatter: "text_default",
list_class: TextFieldItemList::class,
)]
class TextItem extends TextItemBase {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
return [
'max_length' => 255,
] + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => $field_definition->getSetting('max_length'),
],
'format' => [
'type' => 'varchar',
'length' => 255,
],
],
'indexes' => [
'format' => ['format'],
],
];
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints = parent::getConstraints();
if ($max_length = $this->getSetting('max_length')) {
$constraints[] = $constraint_manager->create('ComplexData', [
'value' => [
'Length' => [
'max' => $max_length,
'maxMessage' => $this->t('%name: the text may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
],
],
]);
}
return $constraints;
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$element['max_length'] = [
'#type' => 'number',
'#title' => $this->t('Maximum length'),
'#default_value' => $this->getSetting('max_length'),
'#required' => TRUE,
'#description' => $this->t('The maximum length of the field in characters.'),
'#min' => 1,
'#disabled' => $has_data,
];
$element += parent::storageSettingsForm($form, $form_state, $has_data);
return $element;
}
}

View File

@ -0,0 +1,153 @@
<?php
namespace Drupal\text\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Base class for 'text' configurable field types.
*/
abstract class TextItemBase extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return ['allowed_formats' => []] + parent::defaultFieldSettings();
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this->getSettings();
$element['allowed_formats'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Allowed text formats'),
'#options' => $this->get('format')->getPossibleOptions(),
'#default_value' => !empty($settings['allowed_formats']) ? $settings['allowed_formats'] : [],
'#description' => $this->t('Select the allowed text formats. If no formats are selected, all available text formats will be displayed to the user.'),
'#element_validate' => [[static::class, 'validateAllowedFormats']],
];
return $element;
}
/**
* Render API callback: Processes the allowed formats value.
*
* Ensure the element's value is an indexed array of selected format IDs.
* This function is assigned as an #element_validate callback.
*
* @see static::fieldSettingsForm()
*/
public static function validateAllowedFormats(array &$element, FormStateInterface $form_state) {
$value = array_values(array_filter($form_state->getValue($element['#parents'])));
$form_state->setValueForElement($element, $value);
}
/**
* {@inheritdoc}
*/
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
// Add explicitly allowed formats as config dependencies.
$format_dependencies = [];
$dependencies = parent::calculateDependencies($field_definition);
if (!is_null($field_definition->getSetting('allowed_formats'))) {
$format_dependencies = array_map(function (string $format_id) {
return 'filter.format.' . $format_id;
}, $field_definition->getSetting('allowed_formats'));
}
$config = $dependencies['config'] ?? [];
$dependencies['config'] = array_merge($config, $format_dependencies);
return $dependencies;
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Text'))
->setRequired(TRUE);
$properties['format'] = DataDefinition::create('filter_format')
->setLabel(t('Text format'))
->setSetting('allowed_formats', $field_definition->getSetting('allowed_formats'));
$properties['processed'] = DataDefinition::create('string')
->setLabel(t('Processed text'))
->setDescription(t('The text with the text format applied.'))
->setComputed(TRUE)
->setClass('\Drupal\text\TextProcessed')
->setSetting('text source', 'value')
->setInternal(FALSE);
return $properties;
}
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
// @todo Add in the filter default format here.
$this->setValue(['format' => NULL], $notify);
return $this;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function onChange($property_name, $notify = TRUE) {
// Unset processed properties that are affected by the change.
foreach ($this->definition->getPropertyDefinitions() as $property => $definition) {
if ($definition->getClass() == '\Drupal\text\TextProcessed') {
if ($property_name == 'format' || ($definition->getSetting('text source') == $property_name)) {
$this->writePropertyValue($property, NULL);
}
}
}
parent::onChange($property_name, $notify);
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$settings = $field_definition->getSettings();
if (empty($settings['max_length'])) {
// Textarea handling
$value = $random->paragraphs();
}
else {
// Textfield handling.
$max = (int) ceil($settings['max_length'] / 3);
$value = substr($random->sentences(mt_rand(1, $max), FALSE), 0, $settings['max_length']);
}
$values = [
'value' => $value,
'summary' => $value,
'format' => filter_fallback_format(),
];
return $values;
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace Drupal\text\Plugin\Field\FieldType;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'text_long' field type.
*/
#[FieldType(
id: "text_long",
label: new TranslatableMarkup("Text (formatted, long)"),
description: [
new TranslatableMarkup("Ideal for longer texts, like body or description without a summary"),
new TranslatableMarkup("Supports long text without specifying a maximum length"),
new TranslatableMarkup("May use more storage and be slower for searching and sorting"),
],
category: "formatted_text",
default_widget: "text_textarea",
default_formatter: "text_default",
list_class: TextFieldItemList::class,
)]
class TextLongItem extends TextItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'text',
'size' => 'big',
],
'format' => [
'type' => 'varchar_ascii',
'length' => 255,
],
],
'indexes' => [
'format' => ['format'],
],
];
}
}

View File

@ -0,0 +1,134 @@
<?php
namespace Drupal\text\Plugin\Field\FieldType;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'text_with_summary' field type.
*/
#[FieldType(
id: "text_with_summary",
label: new TranslatableMarkup("Text (formatted, long, with summary)"),
description: [
new TranslatableMarkup("Ideal for longer texts, like body or description with a summary"),
new TranslatableMarkup("Allows specifying a summary for the text"),
new TranslatableMarkup("Supports long text without specifying a maximum length"),
new TranslatableMarkup("May use more storage and be slower for searching and sorting"),
],
category: "formatted_text",
default_widget: "text_textarea_with_summary",
default_formatter: "text_default",
list_class: TextFieldItemList::class,
)]
class TextWithSummaryItem extends TextItemBase {
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return [
'display_summary' => 0,
'required_summary' => FALSE,
] + parent::defaultFieldSettings();
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
$properties['summary'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Summary'));
$properties['summary_processed'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Processed summary'))
->setDescription(new TranslatableMarkup('The summary text with the text format applied.'))
->setComputed(TRUE)
->setClass('\Drupal\text\TextProcessed')
->setSetting('text source', 'summary');
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'text',
'size' => 'big',
],
'summary' => [
'type' => 'text',
'size' => 'big',
],
'format' => [
'type' => 'varchar_ascii',
'length' => 255,
],
],
'indexes' => [
'format' => ['format'],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('summary')->getValue();
return parent::isEmpty() && ($value === NULL || $value === '');
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this->getSettings();
$element['display_summary'] = [
'#type' => 'checkbox',
'#title' => $this->t('Summary input'),
'#default_value' => $settings['display_summary'],
'#description' => $this->t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'),
];
$element['required_summary'] = [
'#type' => 'checkbox',
'#title' => $this->t('Require summary'),
'#description' => $this->t('The summary will also be visible when marked as required.'),
'#default_value' => $settings['required_summary'],
];
return $element;
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
if ($this->getSetting('required_summary')) {
$manager = $this->getTypedDataManager()->getValidationConstraintManager();
$constraints[] = $manager->create('ComplexData', [
'summary' => [
'NotNull' => [
'message' => $this->t('The summary field is required for @name', ['@name' => $this->getFieldDefinition()->getLabel()]),
],
],
]);
}
return $constraints;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Drupal\text\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
* Plugin implementation of the 'text_textarea' widget.
*/
#[FieldWidget(
id: 'text_textarea',
label: new TranslatableMarkup('Text area (multiple rows)'),
field_types: ['text_long'],
)]
class TextareaWidget extends StringTextareaWidget {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['rows']['#description'] = $this->t('Text editors may override this setting.');
return $element;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$main_widget = parent::formElement($items, $delta, $element, $form, $form_state);
$allowed_formats = $this->getFieldSetting('allowed_formats');
$element = $main_widget['value'];
$element['#type'] = 'text_format';
$element['#format'] = $items[$delta]->format;
$element['#base_type'] = $main_widget['value']['#type'];
if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) {
$element['#allowed_formats'] = $allowed_formats;
}
return $element;
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
if (isset($element['format']['#access']) && !$element['format']['#access'] && preg_match('/^[0-9]*\.format$/', $violation->getPropertyPath())) {
// Ignore validation errors for formats if formats may not be changed,
// such as when existing formats become invalid.
// See \Drupal\filter\Element\TextFormat::processFormat().
return FALSE;
}
return $element;
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace Drupal\text\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'text_textarea_with_summary' widget.
*/
#[FieldWidget(
id: 'text_textarea_with_summary',
label: new TranslatableMarkup('Text area with a summary'),
field_types: ['text_with_summary'],
)]
class TextareaWithSummaryWidget extends TextareaWidget {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'rows' => '9',
'summary_rows' => '3',
'placeholder' => '',
'show_summary' => FALSE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['summary_rows'] = [
'#type' => 'number',
'#title' => $this->t('Summary rows'),
'#default_value' => $this->getSetting('summary_rows'),
'#description' => $element['rows']['#description'],
'#required' => TRUE,
'#min' => 1,
];
$element['show_summary'] = [
'#type' => 'checkbox',
'#title' => $this->t('Always show the summary field'),
'#default_value' => $this->getSetting('show_summary'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this->t('Number of summary rows: @rows', ['@rows' => $this->getSetting('summary_rows')]);
if ($this->getSetting('show_summary')) {
$summary[] = $this->t('Summary field will always be visible');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
$required = empty($form['#type']) && $this->getFieldSetting('required_summary');
$element['summary'] = [
'#type' => $display_summary ? 'textarea' : 'value',
'#default_value' => $items[$delta]->summary,
'#title' => $this->t('Summary'),
'#rows' => $this->getSetting('summary_rows'),
'#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
'#attributes' => ['class' => ['text-summary']],
'#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
'#suffix' => '</div>',
'#weight' => -10,
'#required' => $required,
];
if (!$this->getSetting('show_summary') && !$required) {
$element['summary']['#attributes']['class'][] = 'js-text-summary';
$element['summary']['#attached']['library'][] = 'text/drupal.text';
}
return $element;
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
$element = parent::errorElement($element, $violation, $form, $form_state);
$property_path_array = explode('.', $violation->getPropertyPath());
return ($element === FALSE) ? FALSE : $element[$property_path_array[1]];
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Drupal\text\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
* Plugin implementation of the 'text_textfield' widget.
*/
#[FieldWidget(
id: 'text_textfield',
label: new TranslatableMarkup('Text field'),
field_types: ['text'],
)]
class TextfieldWidget extends StringTextfieldWidget {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$main_widget = parent::formElement($items, $delta, $element, $form, $form_state);
$allowed_formats = $this->getFieldSetting('allowed_formats');
$element = $main_widget['value'];
$element['#type'] = 'text_format';
$element['#format'] = $items[$delta]->format ?? NULL;
$element['#base_type'] = $main_widget['value']['#type'];
if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) {
$element['#allowed_formats'] = $allowed_formats;
}
return $element;
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
if (isset($element['format']['#access']) && !$element['format']['#access'] && preg_match('/^[0-9]*\.format$/', $violation->getPropertyPath())) {
// Ignore validation errors for formats that may not be changed,
// such as when existing formats become invalid.
// See \Drupal\filter\Element\TextFormat::processFormat().
return FALSE;
}
return $element;
}
}

View File

@ -0,0 +1,138 @@
<?php
namespace Drupal\text\Plugin\migrate\field\d6;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Attribute\MigrateField;
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
// cspell:ignore optionwidgets
/**
* MigrateField Plugin for Drupal 6 text fields.
*/
#[MigrateField(
id: 'd6_text',
core: [6],
type_map: [
'text' => 'text',
'text_long' => 'text_long',
'text_with_summary' => 'text_with_summary',
],
source_module: 'text',
destination_module: 'text',
)]
class TextField extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function getFieldWidgetMap() {
return [
'text_textfield' => 'text_textfield',
];
}
/**
* {@inheritdoc}
*/
public function getFieldFormatterMap() {
return [
'default' => 'text_default',
'trimmed' => 'text_trimmed',
'plain' => 'basic_string',
];
}
/**
* {@inheritdoc}
*/
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $field_info) {
$widget_type = $field_info['widget_type'] ?? $field_info['widget']['type'];
if ($widget_type == 'optionwidgets_onoff') {
$process = [
'value' => [
'plugin' => 'static_map',
'source' => 'value',
'default_value' => 0,
],
];
$checked_value = explode("\n", $field_info['global_settings']['allowed_values'])[1];
if (str_contains($checked_value, '|')) {
$checked_value = substr($checked_value, 0, strpos($checked_value, '|'));
}
$process['value']['map'][$checked_value] = 1;
}
else {
// See \Drupal\migrate_drupal\Plugin\migrate\source\d6\User::baseFields(),
// signature_format for an example of the YAML that represents this
// process array.
$process = [
'value' => 'value',
'format' => [
[
'plugin' => 'static_map',
'bypass' => TRUE,
'source' => 'format',
'map' => [0 => NULL],
],
[
'plugin' => 'skip_on_empty',
'method' => 'process',
],
[
'plugin' => 'migration_lookup',
'migration' => [
'd6_filter_format',
'd7_filter_format',
],
'source' => 'format',
],
],
];
}
$process = [
'plugin' => 'sub_process',
'source' => $field_name,
'process' => $process,
];
$migration->setProcessOfProperty($field_name, $process);
}
/**
* {@inheritdoc}
*/
public function getFieldType(Row $row) {
$widget_type = $row->getSourceProperty('widget_type');
$settings = $row->getSourceProperty('global_settings');
if ($widget_type == 'text_textfield') {
$field_type = $settings['text_processing'] ? 'text' : 'string';
if (empty($settings['max_length']) || $settings['max_length'] > 255) {
$field_type .= '_long';
}
return $field_type;
}
if ($widget_type == 'text_textarea') {
$field_type = $settings['text_processing'] ? 'text_long' : 'string_long';
return $field_type;
}
switch ($widget_type) {
case 'optionwidgets_buttons':
case 'optionwidgets_select':
return 'list_string';
case 'optionwidgets_onoff':
return 'boolean';
default:
return parent::getFieldType($row);
}
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace Drupal\text\Plugin\migrate\field\d7;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate_drupal\Attribute\MigrateField;
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
/**
* Migrate field plugin for Drupal 7 text fields.
*/
#[MigrateField(
id: 'd7_text',
core: [7],
type_map: [
'text' => 'text',
'text_long' => 'text_long',
'text_with_summary' => 'text_with_summary',
],
source_module: 'text',
destination_module: 'text',
)]
class TextField extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function getFieldFormatterType(Row $row) {
$field_type = $this->getFieldType($row);
$formatter_type = $row->getSourceProperty('formatter/type');
switch ($field_type) {
case 'string':
$formatter_type = str_replace(['text_default', 'text_plain'], 'string', $formatter_type);
break;
case 'string_long':
$formatter_type = str_replace(['text_default', 'text_plain'], 'basic_string', $formatter_type);
break;
}
return $formatter_type;
}
/**
* {@inheritdoc}
*/
public function getFieldWidgetType(Row $row) {
$field_type = $this->getFieldType($row);
$widget_type = $row->getSourceProperty('widget/type');
switch ($field_type) {
case 'string':
$widget_type = str_replace('text_textfield', 'string_textfield', $widget_type);
break;
case 'string_long':
$widget_type = str_replace('text_textarea', 'string_textarea', $widget_type);
break;
}
return $widget_type;
}
/**
* {@inheritdoc}
*/
public function getFieldType(Row $row) {
$type = $row->getSourceProperty('type');
$plain_text = FALSE;
$filtered_text = FALSE;
foreach ($row->getSourceProperty('instances') as $instance) {
// Check if this field has plain text instances, filtered text instances,
// or both.
$data = unserialize($instance['data']);
switch ($data['settings']['text_processing']) {
case '0':
$plain_text = TRUE;
break;
case '1':
$filtered_text = TRUE;
break;
}
}
if (in_array($type, ['text', 'text_long'])) {
// If a text or text_long field has only plain text instances, migrate it
// to a string or string_long field.
if ($plain_text && !$filtered_text) {
$type = str_replace(['text', 'text_long'], ['string', 'string_long'], $type);
}
// If a text or text_long field has both plain text and filtered text
// instances, skip the row.
elseif ($plain_text && $filtered_text) {
$field_name = $row->getSourceProperty('field_name');
throw new MigrateSkipRowException("Can't migrate source field $field_name configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text");
}
}
elseif ($type == 'text_with_summary' && $plain_text) {
// If a text_with_summary field has plain text instances, skip the row
// since there's no such thing as a string_with_summary field.
$field_name = $row->getSourceProperty('field_name');
throw new MigrateSkipRowException("Can't migrate source field $field_name of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text");
}
return $type;
}
}

View File

@ -0,0 +1,115 @@
<?php
namespace Drupal\text;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Serialization\Attribute\JsonSchema;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Render\FilteredMarkup;
/**
* A computed property for processing text with a format.
*
* Required settings (below the definition's 'settings' key) are:
* - text source: The text property containing the to be processed text.
*/
class TextProcessed extends TypedData implements CacheableDependencyInterface {
/**
* Cached processed text.
*
* @var \Drupal\filter\FilterProcessResult|null
*/
protected $processed = NULL;
/**
* {@inheritdoc}
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, ?TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
if ($definition->getSetting('text source') === NULL) {
throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
}
}
/**
* {@inheritdoc}
*/
#[JsonSchema(['type' => 'string', 'description' => 'May contain HTML markup.'])]
public function getValue() {
if ($this->processed !== NULL) {
return FilteredMarkup::create($this->processed->getProcessedText());
}
$item = $this->getParent();
$text = $item->{($this->definition->getSetting('text source'))};
// Avoid doing unnecessary work on empty strings.
if (!isset($text) || $text === '') {
$this->processed = new FilterProcessResult('');
}
else {
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $item->format,
'#filter_types_to_skip' => [],
'#langcode' => $item->getLangcode(),
];
// Capture the cacheability metadata associated with the processed text.
$processed_text = $this->getRenderer()->renderInIsolation($build);
$this->processed = FilterProcessResult::createFromRenderArray($build)->setProcessedText((string) $processed_text);
}
return FilteredMarkup::create($this->processed->getProcessedText());
}
/**
* {@inheritdoc}
*/
public function setValue($value, $notify = TRUE) {
$this->processed = $value;
// Notify the parent of any changes.
if ($notify && isset($this->parent)) {
$this->parent->onChange($this->name);
}
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
$this->getValue();
return $this->processed->getCacheTags();
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$this->getValue();
return $this->processed->getCacheContexts();
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
$this->getValue();
return $this->processed->getCacheMaxAge();
}
/**
* Returns the renderer service.
*
* @return \Drupal\Core\Render\RendererInterface
* The renderer service.
*/
protected function getRenderer() {
return \Drupal::service('renderer');
}
}