first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_default' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "text_default",
|
||||
* label = @Translation("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;
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldFormatter;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_summary_or_trimmed' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "text_summary_or_trimmed",
|
||||
* label = @Translation("Summary or trimmed"),
|
||||
* field_types = {
|
||||
* "text_with_summary"
|
||||
* },
|
||||
* quickedit = {
|
||||
* "editor" = "form"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TextSummaryOrTrimmedFormatter extends TextTrimmedFormatter {}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Security\TrustedCallbackInterface;
|
||||
|
||||
/**
|
||||
* 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 = @Translation("Trimmed"),
|
||||
* field_types = {
|
||||
* "text",
|
||||
* "text_long",
|
||||
* "text_with_summary"
|
||||
* },
|
||||
* quickedit = {
|
||||
* "editor" = "form"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
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' => t('Trimmed limit'),
|
||||
'#type' => 'number',
|
||||
'#field_suffix' => t('characters'),
|
||||
'#default_value' => $this->getSetting('trim_length'),
|
||||
'#description' => 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[] = 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 as a 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'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "text",
|
||||
* label = @Translation("Text (formatted)"),
|
||||
* description = @Translation("This field stores a text with a text format."),
|
||||
* category = @Translation("Text"),
|
||||
* default_widget = "text_textfield",
|
||||
* default_formatter = "text_default"
|
||||
* )
|
||||
*/
|
||||
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' => 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' => t('Maximum length'),
|
||||
'#default_value' => $this->getSetting('max_length'),
|
||||
'#required' => TRUE,
|
||||
'#description' => t('The maximum length of the field in characters.'),
|
||||
'#min' => 1,
|
||||
'#disabled' => $has_data,
|
||||
];
|
||||
$element += parent::storageSettingsForm($form, $form_state, $has_data);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?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\TypedData\DataDefinition;
|
||||
|
||||
/**
|
||||
* Base class for 'text' configurable field types.
|
||||
*/
|
||||
abstract class TextItemBase extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* {@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'));
|
||||
|
||||
$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.
|
||||
$value = substr($random->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']);
|
||||
}
|
||||
|
||||
$values = [
|
||||
'value' => $value,
|
||||
'summary' => $value,
|
||||
'format' => filter_fallback_format(),
|
||||
];
|
||||
return $values;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_long' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "text_long",
|
||||
* label = @Translation("Text (formatted, long)"),
|
||||
* description = @Translation("This field stores a long text with a text format."),
|
||||
* category = @Translation("Text"),
|
||||
* default_widget = "text_textarea",
|
||||
* default_formatter = "text_default"
|
||||
* )
|
||||
*/
|
||||
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'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_with_summary' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "text_with_summary",
|
||||
* label = @Translation("Text (formatted, long, with summary)"),
|
||||
* description = @Translation("This field stores long text with a format and an optional summary."),
|
||||
* category = @Translation("Text"),
|
||||
* default_widget = "text_textarea_with_summary",
|
||||
* default_formatter = "text_default"
|
||||
* )
|
||||
*/
|
||||
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(t('Summary'));
|
||||
|
||||
$properties['summary_processed'] = DataDefinition::create('string')
|
||||
->setLabel(t('Processed summary'))
|
||||
->setDescription(t('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 = [];
|
||||
$settings = $this->getSettings();
|
||||
|
||||
$element['display_summary'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Summary input'),
|
||||
'#default_value' => $settings['display_summary'],
|
||||
'#description' => 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' => t('Require summary'),
|
||||
'#description' => 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_textarea' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "text_textarea",
|
||||
* label = @Translation("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 (like CKEditor) 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);
|
||||
|
||||
$element = $main_widget['value'];
|
||||
$element['#type'] = 'text_format';
|
||||
$element['#format'] = $items[$delta]->format;
|
||||
$element['#base_type'] = $main_widget['value']['#type'];
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
|
||||
if ($violation->arrayPropertyPath == ['format'] && isset($element['format']['#access']) && !$element['format']['#access']) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
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 = @Translation("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' => t('Summary rows'),
|
||||
'#default_value' => $this->getSetting('summary_rows'),
|
||||
'#description' => $element['rows']['#description'],
|
||||
'#required' => TRUE,
|
||||
'#min' => 1,
|
||||
];
|
||||
$element['show_summary'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Always show the summary field'),
|
||||
'#default_value' => $this->getSetting('show_summary'),
|
||||
];
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = parent::settingsSummary();
|
||||
|
||||
$summary[] = t('Number of summary rows: @rows', ['@rows' => $this->getSetting('summary_rows')]);
|
||||
if ($this->getSetting('show_summary')) {
|
||||
$summary[] = 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' => 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' => ['js-text-summary', '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']['#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);
|
||||
return ($element === FALSE) ? FALSE : $element[$violation->arrayPropertyPath[0]];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'text_textfield' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "text_textfield",
|
||||
* label = @Translation("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);
|
||||
|
||||
$element = $main_widget['value'];
|
||||
$element['#type'] = 'text_format';
|
||||
$element['#format'] = isset($items[$delta]->format) ? $items[$delta]->format : NULL;
|
||||
$element['#base_type'] = $main_widget['value']['#type'];
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
|
||||
if ($violation->arrayPropertyPath == ['format'] && isset($element['format']['#access']) && !$element['format']['#access']) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\migrate\cckfield;
|
||||
|
||||
@trigger_error('TextField is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\text\Plugin\migrate\field\d6\TextField or \Drupal\text\Plugin\migrate\field\d7\TextField instead.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\cckfield\CckFieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateCckField(
|
||||
* id = "text",
|
||||
* type_map = {
|
||||
* "text" = "text",
|
||||
* "text_long" = "text_long",
|
||||
* "text_with_summary" = "text_with_summary"
|
||||
* },
|
||||
* core = {6,7},
|
||||
* source_module = "text",
|
||||
* destination_module = "text",
|
||||
* )
|
||||
*
|
||||
* @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
|
||||
* \Drupal\text\Plugin\migrate\field\d6\TextField or
|
||||
* \Drupal\text\Plugin\migrate\field\d7\TextField instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2751897
|
||||
*/
|
||||
class TextField extends CckFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@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 processCckFieldValues(MigrationInterface $migration, $field_name, $field_info) {
|
||||
$widget_type = isset($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 (strpos($checked_value, '|') !== FALSE) {
|
||||
$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',
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\migrate\field\d6;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "d6_text",
|
||||
* type_map = {
|
||||
* "text" = "text",
|
||||
* "text_long" = "text_long",
|
||||
* "text_with_summary" = "text_with_summary"
|
||||
* },
|
||||
* core = {6},
|
||||
* 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 = isset($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 (strpos($checked_value, '|') !== FALSE) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text\Plugin\migrate\field\d7;
|
||||
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\migrate\MigrateSkipRowException;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "d7_text",
|
||||
* type_map = {
|
||||
* "text" = "text",
|
||||
* "text_long" = "text_long",
|
||||
* "text_with_summary" = "text_with_summary"
|
||||
* },
|
||||
* core = {7},
|
||||
* 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', '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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\text;
|
||||
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
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}
|
||||
*/
|
||||
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()->renderPlain($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
|
||||
*/
|
||||
protected function getRenderer() {
|
||||
return \Drupal::service('renderer');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user