Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,51 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter.
*/
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",
* },
* quickedit = {
* "editor" = "plain_text"
* }
* )
*/
class TextDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
// The ProcessedText element already handles cache context & tag bubbling.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item->getLangcode(),
);
}
return $elements;
}
}

View file

@ -0,0 +1,24 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldFormatter\TextSummaryOrTrimmedFormatter.
*/
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 { }

View file

@ -0,0 +1,131 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldFormatter\TextTrimmedFormatter.
*/
namespace Drupal\text\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* 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 {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'trim_length' => '600',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['trim_length'] = array(
'#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.', array('%label' => $this->fieldDefinition->getLabel())),
'#min' => 1,
'#required' => TRUE,
);
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary[] = t('Trimmed limit: @trim_length characters', array('@trim_length' => $this->getSetting('trim_length')));
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$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'][] = '\Drupal\text\Plugin\field\FieldFormatter\TextTrimmedFormatter::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] = array(
'#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;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldType\TextItem.
*/
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 array(
'max_length' => 255,
) + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'varchar',
'length' => $field_definition->getSetting('max_length'),
),
'format' => array(
'type' => 'varchar',
'length' => 255,
),
),
'indexes' => array(
'format' => array('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', array(
'value' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)),
)
),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = array();
$element['max_length'] = array(
'#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;
}
}

View file

@ -0,0 +1,99 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldType\TextItemBase.
*/
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');
return $properties;
}
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
// Default to a simple \Drupal\Component\Utility\SafeMarkup::checkPlain().
// @todo: Add in the filter default format here.
$this->setValue(array('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 = array(
'value' => $value,
'summary' => $value,
'format' => filter_fallback_format(),
);
return $values;
}
}

View file

@ -0,0 +1,47 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldType\TextLongItem.
*/
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 array(
'columns' => array(
'value' => array(
'type' => 'text',
'size' => 'big',
),
'format' => array(
'type' => 'varchar_ascii',
'length' => 255,
),
),
'indexes' => array(
'format' => array('format'),
),
);
}
}

View file

@ -0,0 +1,106 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldType\TextWithSummaryItem.
*/
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 array(
'display_summary' => 0,
) + 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 array(
'columns' => array(
'value' => array(
'type' => 'text',
'size' => 'big',
),
'summary' => array(
'type' => 'text',
'size' => 'big',
),
'format' => array(
'type' => 'varchar_ascii',
'length' => 255,
),
),
'indexes' => array(
'format' => array('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 = array();
$settings = $this->getSettings();
$element['display_summary'] = array(
'#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.'),
);
return $element;
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldWidget\TextareaWidget.
*/
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 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 == array('format') && isset($element['format']['#access']) && !$element['format']['#access']) {
// Ignore validation errors for formats if formats may not be changed,
// i.e. when existing formats become invalid. See filter_process_format().
return FALSE;
}
return $element;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldWidget\TextareaWithSummaryWidget.
*/
namespace Drupal\text\Plugin\Field\FieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\text\Plugin\Field\FieldWidget\TextareaWidget;
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 array(
'rows' => '9',
'summary_rows' => '3',
'placeholder' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['summary_rows'] = array(
'#type' => 'number',
'#title' => t('Summary rows'),
'#default_value' => $this->getSetting('summary_rows'),
'#required' => TRUE,
'#min' => 1,
);
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = t('Number of summary rows: !rows', array('!rows' => $this->getSetting('summary_rows')));
return $summary;
}
/**
* {@inheritdoc}
*/
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');
$element['summary'] = array(
'#type' => $display_summary ? 'textarea' : 'value',
'#default_value' => $items[$delta]->summary,
'#title' => t('Summary'),
'#rows' => $this->getSetting('summary_rows'),
'#description' => t('Leave blank to use trimmed value of full text as the summary.'),
'#attached' => array(
'library' => array('text/drupal.text'),
),
'#attributes' => array('class' => array('js-text-summary', 'text-summary')),
'#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
'#suffix' => '</div>',
'#weight' => -10,
);
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]];
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\text\Plugin\Field\FieldWidget\TextfieldWidget.
*/
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 == array('format') && isset($element['format']['#access']) && !$element['format']['#access']) {
// Ignore validation errors for formats if formats may not be changed,
// i.e. when existing formats become invalid. See filter_process_format().
return FALSE;
}
return $element;
}
}