Skip to content
Snippets Groups Projects

Correcting coding standards

1 file
+ 351
154
Compare changes
  • Side-by-side
  • Inline
@@ -17,6 +17,14 @@ use Drupal\field_group\FormatterHelper;
use Drupal\paragraphs\ParagraphInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Utility\Token;
/**
* Plugin implementation of the 'entity_reference paragraphs' widget.
@@ -77,78 +85,186 @@ class InlineParagraphsWidget extends WidgetBase {
*/
protected $accessOptions = NULL;
/**
* Entity display repository service.
*
* @var Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Entity type manager service.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Content Translation Manager service.
*
* @var Drupal\content_translation\ContentTranslationManagerInterface
*/
protected $contentTranslationManager;
/**
* Entity type bundle info service.
*
* @var Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Selection plugin manager service.
*
* @var Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface
*/
protected $selectionPluginManager;
/**
* Module handler service.
*
* @var Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Token service.
*
* @var Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructor.
*
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $settings
* The formatter settings.
* @param array $third_party_settings
* Any third party settings.
* @param Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
* Entity display repository service.
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager service.
* @param Drupal\content_translation\ContentTranslationManagerInterface $contentTranslationManager
* Content Translation Manager service.
* @param Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* Entity type bundle info service.
* @param Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
* Selection plugin manager service.
* @param Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* Module handler service.
* @param Drupal\Core\Utility\Token $token
* Token service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityDisplayRepositoryInterface $entityDisplayRepository, EntityTypeManagerInterface $entityTypeManager, ContentTranslationManagerInterface $contentTranslationManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, SelectionPluginManagerInterface $selectionPluginManager, ModuleHandlerInterface $moduleHandler, Token $token) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityDisplayRepository = $entityDisplayRepository;
$this->entityTypeManager = $entityTypeManager;
$this->contentTranslationManager = $contentTranslationManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->selectionPluginManager = $selectionPluginManager;
$this->moduleHandler = $moduleHandler;
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('entity_display.repository'),
$container->get('entity_type.manager'),
$container->get('content_translation.manager'),
$container->get('entity_type.bundle.info'),
$container->get('plugin.manager.entity_reference_selection'),
$container->get('module_handler'),
$container->get('token'),
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
return [
'title' => t('Paragraph'),
'title_plural' => t('Paragraphs'),
'edit_mode' => 'open',
'add_mode' => 'dropdown',
'form_display_mode' => 'default',
'default_paragraph_type' => '',
);
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = array();
$elements = [];
$elements['title'] = array(
$elements['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Paragraph Title'),
'#description' => $this->t('Label to appear as title on the button as "Add new [title]", this label is translatable'),
'#default_value' => $this->getSetting('title'),
'#required' => TRUE,
);
];
$elements['title_plural'] = array(
$elements['title_plural'] = [
'#type' => 'textfield',
'#title' => $this->t('Plural Paragraph Title'),
'#description' => $this->t('Title in its plural form.'),
'#default_value' => $this->getSetting('title_plural'),
'#required' => TRUE,
);
];
$elements['edit_mode'] = array(
$elements['edit_mode'] = [
'#type' => 'select',
'#title' => $this->t('Edit mode'),
'#description' => $this->t('The mode the paragraph is in by default. Preview will render the paragraph in the preview view mode.'),
'#options' => array(
'#options' => [
'open' => $this->t('Open'),
'closed' => $this->t('Closed'),
'preview' => $this->t('Preview'),
),
],
'#default_value' => $this->getSetting('edit_mode'),
'#required' => TRUE,
);
];
$elements['add_mode'] = array(
$elements['add_mode'] = [
'#type' => 'select',
'#title' => $this->t('Add mode'),
'#description' => $this->t('The way to add new Paragraphs.'),
'#options' => array(
'#options' => [
'select' => $this->t('Select list'),
'button' => $this->t('Buttons'),
'dropdown' => $this->t('Dropdown button')
),
'dropdown' => $this->t('Dropdown button'),
],
'#default_value' => $this->getSetting('add_mode'),
'#required' => TRUE,
);
];
$elements['form_display_mode'] = array(
$elements['form_display_mode'] = [
'#type' => 'select',
'#options' => \Drupal::service('entity_display.repository')->getFormModeOptions($this->getFieldSetting('target_type')),
'#options' => $this->entityDisplayRepository->getFormModeOptions($this->getFieldSetting('target_type')),
'#description' => $this->t('The form display mode to use when rendering the paragraph form.'),
'#title' => $this->t('Form display mode'),
'#default_value' => $this->getSetting('form_display_mode'),
'#required' => TRUE,
);
];
$options = [];
$options = [];
foreach ($this->getAllowedTypes() as $key => $bundle) {
$options[$key] = $bundle['label'];
}
@@ -169,33 +285,37 @@ class InlineParagraphsWidget extends WidgetBase {
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary = [];
$summary[] = $this->t('Title: @title', ['@title' => $this->getSetting('title')]);
$summary[] = $this->t('Plural title: @title_plural', [
'@title_plural' => $this->getSetting('title_plural')
'@title_plural' => $this->getSetting('title_plural'),
]);
switch($this->getSetting('edit_mode')) {
switch ($this->getSetting('edit_mode')) {
case 'open':
default:
$edit_mode = $this->t('Open');
break;
case 'closed':
$edit_mode = $this->t('Closed');
break;
case 'preview':
$edit_mode = $this->t('Preview');
break;
}
switch($this->getSetting('add_mode')) {
switch ($this->getSetting('add_mode')) {
case 'select':
default:
$add_mode = $this->t('Select list');
break;
case 'button':
$add_mode = $this->t('Buttons');
break;
case 'dropdown':
$add_mode = $this->t('Dropdown button');
break;
@@ -204,11 +324,11 @@ class InlineParagraphsWidget extends WidgetBase {
$summary[] = $this->t('Edit mode: @edit_mode', ['@edit_mode' => $edit_mode]);
$summary[] = $this->t('Add mode: @add_mode', ['@add_mode' => $add_mode]);
$summary[] = $this->t('Form display mode: @form_display_mode', [
'@form_display_mode' => $this->getSetting('form_display_mode')
'@form_display_mode' => $this->getSetting('form_display_mode'),
]);
if ($this->getDefaultParagraphTypeLabelName() !== NULL) {
$summary[] = $this->t('Default paragraph type: @default_paragraph_type', [
'@default_paragraph_type' => $this->getDefaultParagraphTypeLabelName()
'@default_paragraph_type' => $this->getDefaultParagraphTypeLabelName(),
]);
}
@@ -231,7 +351,7 @@ class InlineParagraphsWidget extends WidgetBase {
$host = $items->getEntity();
$widget_state = static::getWidgetState($parents, $field_name, $form_state);
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_manager = $this->entityTypeManager;
$target_type = $this->getFieldSetting('target_type');
$item_mode = isset($widget_state['paragraphs'][$delta]['mode']) ? $widget_state['paragraphs'][$delta]['mode'] : 'edit';
@@ -264,9 +384,9 @@ class InlineParagraphsWidget extends WidgetBase {
$entity_type = $entity_type_manager->getDefinition($target_type);
$bundle_key = $entity_type->getKey('bundle');
$paragraphs_entity = $entity_type_manager->getStorage($target_type)->create(array(
$paragraphs_entity = $entity_type_manager->getStorage($target_type)->create([
$bundle_key => $widget_state['selected_bundle'],
));
]);
$paragraphs_entity->setParentEntity($items->getEntity(), $field_name);
$item_mode = 'edit';
@@ -287,11 +407,11 @@ class InlineParagraphsWidget extends WidgetBase {
foreach ($violations as $violation) {
$messages[] = $violation->getMessage();
}
$info['validation_error'] = array(
$info['validation_error'] = [
'#type' => 'container',
'#markup' => $this->t('@messages', ['@messages' => strip_tags(implode('\n', $messages))]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
);
];
}
}
@@ -334,7 +454,7 @@ class InlineParagraphsWidget extends WidgetBase {
// Initialise the translation with source language values.
$paragraphs_entity->addTranslation($langcode, $paragraphs_entity->toArray());
$translation = $paragraphs_entity->getTranslation($langcode);
$manager = \Drupal::service('content_translation.manager');
$manager = $this->contentTranslationManager;
$manager->getTranslationMetadata($translation)->setSource($paragraphs_entity->language()->getId());
}
}
@@ -350,52 +470,52 @@ class InlineParagraphsWidget extends WidgetBase {
$element_parents[] = $delta;
$element_parents[] = 'subform';
$id_prefix = implode('-', array_merge($parents, array($field_name, $delta)));
$id_prefix = implode('-', array_merge($parents, [$field_name, $delta]));
$wrapper_id = Html::getUniqueId($id_prefix . '-item-wrapper');
$element += array(
$element += [
'#type' => 'container',
'#element_validate' => array(array($this, 'elementValidate')),
'#element_validate' => [[$this, 'elementValidate']],
'#paragraph_type' => $paragraphs_entity->bundle(),
'subform' => array(
'subform' => [
'#type' => 'container',
'#parents' => $element_parents,
),
);
],
];
$element['#prefix'] = '<div id="' . $wrapper_id . '">';
$element['#suffix'] = '</div>';
$item_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($target_type);
$item_bundles = $this->entityTypeBundleInfo->getBundleInfo($target_type);
if (isset($item_bundles[$paragraphs_entity->bundle()])) {
$bundle_info = $item_bundles[$paragraphs_entity->bundle()];
$element['top'] = array(
$element['top'] = [
'#type' => 'container',
'#weight' => -1000,
'#attributes' => array(
'class' => array(
'#attributes' => [
'class' => [
'paragraph-type-top',
),
),
);
],
],
];
$element['top']['paragraph_type_title'] = array(
$element['top']['paragraph_type_title'] = [
'#type' => 'container',
'#weight' => 0,
'#attributes' => array(
'class' => array(
'#attributes' => [
'class' => [
'paragraph-type-title',
),
),
);
],
],
];
$element['top']['paragraph_type_title']['info'] = array(
$element['top']['paragraph_type_title']['info'] = [
'#markup' => $bundle_info['label'],
);
];
$actions = array();
$links = array();
$actions = [];
$links = [];
// Avoid checking delete access for new entities.
$delete_access = $paragraphs_entity->isNew() || $paragraphs_entity->access('delete');
@@ -408,7 +528,11 @@ class InlineParagraphsWidget extends WidgetBase {
'#name' => strtr($id_prefix, '-', '_') . '_remove',
'#weight' => 501,
'#submit' => [[get_class($this), 'paragraphsItemSubmit']],
'#limit_validation_errors' => [array_merge($parents, [$field_name, 'add_more'])],
'#limit_validation_errors' => [array_merge(
$parents,
[$field_name, 'add_more']
),
],
'#delta' => $delta,
'#ajax' => [
'callback' => [get_class($this), 'itemAjax'],
@@ -429,108 +553,121 @@ class InlineParagraphsWidget extends WidgetBase {
if ($item_mode == 'edit') {
if (isset($items[$delta]->entity) && ($default_edit_mode == 'preview' || $default_edit_mode == 'closed')) {
$links['collapse_button'] = array(
$links['collapse_button'] = [
'#type' => 'submit',
'#value' => $this->t('Collapse'),
'#name' => strtr($id_prefix, '-', '_') . '_collapse',
'#weight' => 499,
'#submit' => array(array(get_class($this), 'paragraphsItemSubmit')),
'#submit' => [[get_class($this), 'paragraphsItemSubmit']],
'#delta' => $delta,
'#limit_validation_errors' => [array_merge($parents, [$field_name, 'add_more'])],
'#ajax' => array(
'callback' => array(get_class($this), 'itemAjax'),
'#limit_validation_errors' => [array_merge(
$parents,
[$field_name, 'add_more']
),
],
'#ajax' => [
'callback' => [get_class($this), 'itemAjax'],
'wrapper' => $widget_state['ajax_wrapper_id'],
'effect' => 'fade',
),
],
'#access' => $paragraphs_entity->access('update'),
'#prefix' => '<li class="collapse dropbutton__item dropbutton__item--extrasmall">',
'#suffix' => '</li>',
'#paragraphs_mode' => 'collapsed',
'#paragraphs_show_warning' => TRUE,
);
];
}
$info['edit_button_info'] = array(
$info['edit_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to edit this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$paragraphs_entity->access('update') && $delete_access,
);
];
$info['remove_button_info'] = array(
$info['remove_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to remove this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$delete_access && $paragraphs_entity->access('update'),
);
];
$info['edit_remove_button_info'] = array(
$info['edit_remove_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to edit or remove this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$paragraphs_entity->access('update') && !$delete_access,
);
];
}
elseif ($item_mode == 'preview' || $item_mode == 'closed') {
$links['edit_button'] = array(
$links['edit_button'] = [
'#type' => 'submit',
'#value' => $this->t('Edit'),
'#name' => strtr($id_prefix, '-', '_') . '_edit',
'#weight' => 500,
'#submit' => array(array(get_class($this), 'paragraphsItemSubmit')),
'#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))),
'#submit' => [[get_class($this), 'paragraphsItemSubmit']],
'#limit_validation_errors' => [array_merge(
$parents,
[$field_name, 'add_more']
),
],
'#delta' => $delta,
'#ajax' => array(
'callback' => array(get_class($this), 'itemAjax'),
'#ajax' => [
'callback' => [get_class($this), 'itemAjax'],
'wrapper' => $widget_state['ajax_wrapper_id'],
'effect' => 'fade',
),
],
'#access' => $paragraphs_entity->access('update'),
'#prefix' => '<li class="edit dropbutton__item dropbutton__item--extrasmall">',
'#suffix' => '</li>',
'#paragraphs_mode' => 'edit',
);
];
if ($show_must_be_saved_warning) {
$info['must_be_saved_info'] = array(
$info['must_be_saved_info'] = [
'#type' => 'container',
'#markup' => $this->t('You have unsaved changes on this @title item.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You have unsaved changes on this @title item.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
);
];
}
$info['preview_info'] = array(
$info['preview_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to view this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to view this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$paragraphs_entity->access('view'),
);
];
$info['edit_button_info'] = array(
$info['edit_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to edit this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$paragraphs_entity->access('update') && $delete_access,
);
];
$info['remove_button_info'] = array(
$info['remove_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to remove this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$delete_access && $paragraphs_entity->access('update'),
);
];
$info['edit_remove_button_info'] = array(
$info['edit_remove_button_info'] = [
'#type' => 'container',
'#markup' => $this->t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))),
'#markup' => $this->t('You are not allowed to edit or remove this @title.', ['@title' => $this->getSetting('title')]),
'#attributes' => ['class' => ['messages', 'messages--warning']],
'#access' => !$paragraphs_entity->access('update') && !$delete_access,
);
];
}
elseif ($item_mode == 'remove') {
$element['top']['paragraph_type_title']['info'] = [
'#markup' => $this->t('Deleted @title: %type', ['@title' => $this->getSetting('title'), '%type' => $bundle_info['label']]),
'#markup' => $this->t('Deleted @title: %type',
[
'@title' => $this->getSetting('title'),
'%type' => $bundle_info['label'],
]
),
];
$links['confirm_remove_button'] = [
@@ -539,7 +676,13 @@ class InlineParagraphsWidget extends WidgetBase {
'#name' => strtr($id_prefix, '-', '_') . '_confirm_remove',
'#weight' => 503,
'#submit' => [[get_class($this), 'paragraphsItemSubmit']],
'#limit_validation_errors' => [array_merge($parents, [$field_name, 'add_more'])],
'#limit_validation_errors' => [array_merge(
$parents,
[$field_name,
'add_more',
]
),
],
'#delta' => $delta,
'#ajax' => [
'callback' => [get_class($this), 'itemAjax'],
@@ -557,7 +700,13 @@ class InlineParagraphsWidget extends WidgetBase {
'#name' => strtr($id_prefix, '-', '_') . '_restore',
'#weight' => 504,
'#submit' => [[get_class($this), 'paragraphsItemSubmit']],
'#limit_validation_errors' => [array_merge($parents, [$field_name, 'add_more'])],
'#limit_validation_errors' => [array_merge(
$parents,
[$field_name,
'add_more',
]
),
],
'#delta' => $delta,
'#ajax' => [
'callback' => [get_class($this), 'itemAjax'],
@@ -572,7 +721,7 @@ class InlineParagraphsWidget extends WidgetBase {
if (!empty($links)) {
$show_links = 0;
foreach($links as $link_item) {
foreach ($links as $link_item) {
if (!isset($link_item['#access']) || $link_item['#access']) {
$show_links++;
}
@@ -582,19 +731,22 @@ class InlineParagraphsWidget extends WidgetBase {
$element['top']['links'] = $links;
if ($show_links > 1) {
$element['top']['links']['#theme_wrappers'] = array('dropbutton_wrapper', 'paragraphs_dropbutton_wrapper');
$element['top']['links']['prefix'] = array(
$element['top']['links']['#theme_wrappers'] = [
'dropbutton_wrapper',
'paragraphs_dropbutton_wrapper',
];
$element['top']['links']['prefix'] = [
'#markup' => '<ul class="dropbutton dropbutton--multiple dropbutton--extrasmall">',
'#weight' => -999,
);
$element['top']['links']['suffix'] = array(
];
$element['top']['links']['suffix'] = [
'#markup' => '</li>',
'#weight' => 999,
);
];
}
else {
$element['top']['links']['#theme_wrappers'] = array('paragraphs_dropbutton_wrapper');
foreach($links as $key => $link_item) {
$element['top']['links']['#theme_wrappers'] = ['paragraphs_dropbutton_wrapper'];
foreach ($links as $key => $link_item) {
unset($element['top']['links'][$key]['#prefix']);
unset($element['top']['links'][$key]['#suffix']);
}
@@ -605,7 +757,7 @@ class InlineParagraphsWidget extends WidgetBase {
if (!empty($info)) {
$show_info = FALSE;
foreach($info as $info_item) {
foreach ($info as $info_item) {
if (!isset($info_item['#access']) || $info_item['#access']) {
$show_info = TRUE;
break;
@@ -620,7 +772,7 @@ class InlineParagraphsWidget extends WidgetBase {
if (!empty($actions)) {
$show_actions = FALSE;
foreach($actions as $action_item) {
foreach ($actions as $action_item) {
if (!isset($action_item['#access']) || $action_item['#access']) {
$show_actions = TRUE;
break;
@@ -638,7 +790,7 @@ class InlineParagraphsWidget extends WidgetBase {
$display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode'));
// @todo Remove as part of https://www.drupal.org/node/2640056
if (\Drupal::moduleHandler()->moduleExists('field_group')) {
if ($this->moduleHandler->moduleExists('field_group')) {
$context = [
'entity_type' => $paragraphs_entity->getEntityTypeId(),
'bundle' => $paragraphs_entity->bundle(),
@@ -650,7 +802,9 @@ class InlineParagraphsWidget extends WidgetBase {
field_group_attach_groups($element['subform'], $context);
if (method_exists(FormatterHelper::class, 'formProcess')) {
$element['subform']['#process'][] = [FormatterHelper::class, 'formProcess'];
$element['subform']['#process'][] = [
FormatterHelper::class, 'formProcess',
];
}
elseif (function_exists('field_group_form_pre_render')) {
$element['subform']['#pre_render'][] = 'field_group_form_pre_render';
@@ -669,11 +823,11 @@ class InlineParagraphsWidget extends WidgetBase {
$translatable = $paragraphs_entity->{$field}->getFieldDefinition()->isTranslatable();
// Do a check if we have to add a class to the form element. We need
// those classes (paragraphs-content and paragraphs-behavior) to show
// and hide elements, depending of the active perspective.
// those classes (paragraphs-content and paragraphs-behavior)
// to show and hide elements, depending of the active perspective.
// We need them to filter out entity reference revisions fields that
// reference paragraphs, cause otherwise we have problems with showing
// and hiding the right fields in nested paragraphs.
// reference paragraphs, cause otherwise we have problems with
// showing and hiding the right fields in nested paragraphs.
$is_paragraph_field = FALSE;
if ($field_definition->getType() == 'entity_reference_revisions') {
// Check if we are referencing paragraphs.
@@ -691,7 +845,7 @@ class InlineParagraphsWidget extends WidgetBase {
else {
$element['subform'][$field]['widget']['#after_build'][] = [
static::class,
'addTranslatabilityClue'
'addTranslatabilityClue',
];
}
}
@@ -700,13 +854,13 @@ class InlineParagraphsWidget extends WidgetBase {
}
elseif ($item_mode == 'preview') {
$view_builder = $entity_type_manager->getViewBuilder($paragraphs_entity->getEntityTypeId());
$element['subform'] = array();
$element['subform'] = [];
$element['behavior_plugins'] = [];
$element['preview'] = $view_builder->view($paragraphs_entity, 'preview', $paragraphs_entity->language()->getId());
$element['preview']['#access'] = $paragraphs_entity->access('view');
}
elseif ($item_mode == 'closed') {
$element['subform'] = array();
$element['subform'] = [];
$element['behavior_plugins'] = [];
if ($paragraphs_entity) {
$element['top']['paragraph_summary']['fields_info'] = [
@@ -716,7 +870,7 @@ class InlineParagraphsWidget extends WidgetBase {
}
}
else {
$element['subform'] = array();
$element['subform'] = [];
}
$element['subform']['#attributes']['class'][] = 'paragraphs-subform';
@@ -743,31 +897,32 @@ class InlineParagraphsWidget extends WidgetBase {
* Returns the sorted allowed types for a entity reference field.
*
* @return array
* A list of arrays keyed by the paragraph type machine name with the following properties.
* A list of arrays keyed by the paragraph type machine
* name with the following properties.
* - label: The label of the paragraph type.
* - weight: The weight of the paragraph type.
*/
public function getAllowedTypes() {
$return_bundles = array();
$return_bundles = [];
/** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager */
$selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
$selection_manager = $this->selectionPluginManager;
$handler = $selection_manager->getSelectionHandler($this->fieldDefinition);
if ($handler instanceof ParagraphSelection) {
$return_bundles = $handler->getSortedAllowedTypes();
}
// Support for other reference types.
else {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($this->getFieldSetting('target_type'));
$bundles = $this->entityTypeBundleInfo->getBundleInfo($this->getFieldSetting('target_type'));
$weight = 0;
foreach ($bundles as $machine_name => $bundle) {
if (empty($this->getSelectionHandlerSetting('target_bundles'))
|| in_array($machine_name, $this->getSelectionHandlerSetting('target_bundles'))) {
$return_bundles[$machine_name] = array(
$return_bundles[$machine_name] = [
'label' => $bundle['label'],
'weight' => $weight,
);
];
$weight++;
}
@@ -777,6 +932,9 @@ class InlineParagraphsWidget extends WidgetBase {
return $return_bundles;
}
/**
* {@inheritdoc}
*/
public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition->getName();
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
@@ -784,7 +942,7 @@ class InlineParagraphsWidget extends WidgetBase {
$field_state = static::getWidgetState($this->fieldParents, $field_name, $form_state);
$max = $field_state['items_count'];
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_manager = $this->entityTypeManager;
// Consider adding a default paragraph for new host entities.
if ($max == 0 && $items->getEntity()->isNew()) {
@@ -804,7 +962,7 @@ class InlineParagraphsWidget extends WidgetBase {
'entity' => $paragraphs_entity,
'display' => $display,
'mode' => 'edit',
'original_delta' => 1
'original_delta' => 1,
];
$max = 1;
$field_state['items_count'] = $max;
@@ -815,10 +973,10 @@ class InlineParagraphsWidget extends WidgetBase {
$is_multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
$title = $this->fieldDefinition->getLabel();
$description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
$description = FieldFilteredMarkup::create($this->token->replace($this->fieldDefinition->getDescription()));
$elements = array();
$this->fieldIdPrefix = implode('-', array_merge($this->fieldParents, array($field_name)));
$elements = [];
$this->fieldIdPrefix = implode('-', array_merge($this->fieldParents, [$field_name]));
$this->fieldWrapperId = Html::getUniqueId($this->fieldIdPrefix . '-add-more-wrapper');
$elements['#prefix'] = '<div id="' . $this->fieldWrapperId . '">';
$elements['#suffix'] = '</div>';
@@ -835,12 +993,12 @@ class InlineParagraphsWidget extends WidgetBase {
$items->appendItem();
}
// For multiple fields, title and description are handled by the wrapping
// table.
$element = array(
// For multiple fields, title and description are handled by the
// wrapping table.
$element = [
'#title' => $is_multiple ? '' : $title,
'#description' => $is_multiple ? '' : $description,
);
];
$element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
@@ -848,15 +1006,16 @@ class InlineParagraphsWidget extends WidgetBase {
if ($is_multiple) {
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = array(
$element['_weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for row @number', array('@number' => $delta + 1)),
'#title' => $this->t('Weight for row @number', ['@number' => $delta + 1]),
'#title_display' => 'invisible',
// Note: this 'delta' is the FAPI #type 'weight' element's property.
// Note: this 'delta' is the FAPI #type 'weight' element's
// property.
'#delta' => $max,
'#default_value' => $items[$delta]->_weight ?: $delta,
'#weight' => 100,
);
];
}
// Access for the top element is set to FALSE only when the paragraph
@@ -885,12 +1044,12 @@ class InlineParagraphsWidget extends WidgetBase {
];
if ($this->realItemCount > 0) {
$elements += array(
$elements += [
'#theme' => 'field_multiple_value_form',
'#cardinality_multiple' => $is_multiple,
'#title' => $title,
'#description' => $description,
);
];
}
else {
$classes = $this->fieldDefinition->isRequired() ? ['form-required'] : [];
@@ -910,7 +1069,7 @@ class InlineParagraphsWidget extends WidgetBase {
'#markup' => $this->t('No @title added yet.', ['@title' => $this->getSetting('title')]),
'#prefix' => '<em>',
'#suffix' => '</em>',
]
],
],
];
@@ -965,7 +1124,7 @@ class InlineParagraphsWidget extends WidgetBase {
* Add 'add more' button, if not working with a programmed form.
*
* @return array
* The form element array.
* The form element array.
*/
protected function buildAddActions() {
if (count($this->getAccessibleOptions()) === 0) {
@@ -984,7 +1143,7 @@ class InlineParagraphsWidget extends WidgetBase {
];
}
return $add_more_elements ;
return $add_more_elements;
}
if ($this->getSetting('add_mode') == 'button' || $this->getSetting('add_mode') == 'dropdown') {
@@ -1005,7 +1164,7 @@ class InlineParagraphsWidget extends WidgetBase {
return $this->accessOptions;
}
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_manager = $this->entityTypeManager;
$target_type = $this->getFieldSetting('target_type');
$bundles = $this->getAllowedTypes();
$access_control_handler = $entity_type_manager->getAccessControlHandler($target_type);
@@ -1051,7 +1210,7 @@ class InlineParagraphsWidget extends WidgetBase {
'#markup' => '</ul>',
'#weight' => 999,
];
$add_more_elements['#suffix'] = $this->t(' to %type', ['%type' => $title]);
$add_more_elements['#suffix'] = $this->t('to %type', ['%type' => $title]);
}
foreach ($this->getAccessibleOptions() as $machine_name => $label) {
@@ -1060,7 +1219,10 @@ class InlineParagraphsWidget extends WidgetBase {
'#name' => strtr($this->fieldIdPrefix, '-', '_') . '_' . $machine_name . '_add_more',
'#value' => $this->t('Add @type', ['@type' => $label]),
'#attributes' => ['class' => ['field-add-more-submit', 'button--small']],
'#limit_validation_errors' => [array_merge($this->fieldParents, [$field_name, 'add_more'])],
'#limit_validation_errors' => [array_merge(
$this->fieldParents,
[$field_name, 'add_more']),
],
'#submit' => [[get_class($this), 'addMoreSubmit']],
'#ajax' => [
'callback' => [get_class($this), 'addMoreAjax'],
@@ -1106,7 +1268,10 @@ class InlineParagraphsWidget extends WidgetBase {
'#name' => strtr($this->fieldIdPrefix, '-', '_') . '_add_more',
'#value' => $text,
'#attributes' => ['class' => ['field-add-more-submit']],
'#limit_validation_errors' => [array_merge($this->fieldParents, [$field_name, 'add_more'])],
'#limit_validation_errors' => [array_merge(
$this->fieldParents,
[$field_name, 'add_more']),
],
'#submit' => [[get_class($this), 'addMoreSubmit']],
'#ajax' => [
'callback' => [get_class($this), 'addMoreAjax'],
@@ -1115,7 +1280,7 @@ class InlineParagraphsWidget extends WidgetBase {
],
];
$add_more_elements['add_more_button']['#suffix'] = $this->t(' to %type', ['%type' => $title]);
$add_more_elements['add_more_button']['#suffix'] = $this->t('to %type', ['%type' => $title]);
return $add_more_elements;
}
@@ -1127,8 +1292,12 @@ class InlineParagraphsWidget extends WidgetBase {
* is only accessibly through the form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State.
* @param \Drupal\Core\Field\FieldItemListInterface $items
* The Field Item List Interface.
*
* @return string
* Returns Language Code
*/
protected function getCurrentLangcode(FormStateInterface $form_state, FieldItemListInterface $items) {
return $form_state->get('langcode') ?: $items->getEntity()->language()->getId();
@@ -1180,6 +1349,9 @@ class InlineParagraphsWidget extends WidgetBase {
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public static function paragraphsItemSubmit(array $form, FormStateInterface $form_state) {
$button = $form_state->getTriggeringElement();
@@ -1205,6 +1377,9 @@ class InlineParagraphsWidget extends WidgetBase {
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public static function itemAjax(array $form, FormStateInterface $form_state) {
$button = $form_state->getTriggeringElement();
// Go one level up in the form, to the widgets container.
@@ -1241,10 +1416,11 @@ class InlineParagraphsWidget extends WidgetBase {
* Checks whether a content entity is referenced.
*
* @return bool
* Returns Target Type Info
*/
protected function isContentReferenced() {
$target_type = $this->getFieldSetting('target_type');
$target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
$target_type_info = $this->entityTypeManager->getDefinition($target_type);
return $target_type_info->entityClassImplements('\Drupal\Core\Entity\ContentEntityInterface');
}
@@ -1311,7 +1487,7 @@ class InlineParagraphsWidget extends WidgetBase {
$paragraphs_entity = $widget_state['paragraphs'][$item['_original_delta']]['entity'];
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
$display = $widget_state['paragraphs'][$item['_original_delta']]['display'];
$display = $widget_state['paragraphs'][$item['_original_delta']]['display'];
if ($widget_state['paragraphs'][$item['_original_delta']]['mode'] == 'edit') {
$display->extractFormValues($paragraphs_entity, $element[$item['_original_delta']]['subform'], $form_state);
}
@@ -1335,9 +1511,10 @@ class InlineParagraphsWidget extends WidgetBase {
$item['target_revision_id'] = $paragraphs_entity->getRevisionId();
}
// If our mode is remove don't save or reference this entity.
// @todo: Maybe we should actually delete it here?
elseif(isset($widget_state['paragraphs'][$item['_original_delta']]['mode'])
&& in_array($widget_state['paragraphs'][$item['_original_delta']]['mode'], ['remove', 'removed'])) {
// @todo Maybe we should actually delete it here?
elseif (isset($widget_state['paragraphs'][$item['_original_delta']]['mode'])
&& in_array($widget_state['paragraphs'][$item['_original_delta']]['mode'],
['remove', 'removed'])) {
$item['target_id'] = NULL;
$item['target_revision_id'] = NULL;
}
@@ -1358,7 +1535,9 @@ class InlineParagraphsWidget extends WidgetBase {
* Initializes the translation form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State.
* @param \Drupal\Core\Entity\EntityInterface $host
* The host.
*/
protected function initIsTranslating(FormStateInterface $form_state, EntityInterface $host) {
if ($this->isTranslating != NULL) {
@@ -1393,9 +1572,12 @@ class InlineParagraphsWidget extends WidgetBase {
* "(all languages)" suffix to the widget title, replicate that here.
*
* @param array $element
* The element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State.
*
* @return array
* Returns the element.
*/
public static function addTranslatabilityClue(array $element, FormStateInterface $form_state) {
static $suffix, $fapi_title_elements;
@@ -1406,7 +1588,22 @@ class InlineParagraphsWidget extends WidgetBase {
// Elements which can have a #title attribute according to FAPI Reference.
if (!isset($suffix)) {
$suffix = ' <span class="translation-entity-all-languages">(' . t('all languages') . ')</span>';
$fapi_title_elements = array_flip(['checkbox', 'checkboxes', 'date', 'details', 'fieldset', 'file', 'item', 'password', 'password_confirm', 'radio', 'radios', 'select', 'textarea', 'textfield', 'weight']);
$fapi_title_elements = array_flip([
'checkbox',
'checkboxes',
'date',
'details',
'fieldset',
'file',
'item', 'password',
'password_confirm',
'radio',
'radios',
'select',
'textarea',
'textfield',
'weight',
]);
}
// Update #title attribute for all elements that are allowed to have a
@@ -1433,10 +1630,10 @@ class InlineParagraphsWidget extends WidgetBase {
/**
* Returns the default paragraph type.
*
* @return string $default_paragraph_type
* @return string
* Label name for default paragraph type.
*/
protected function getDefaultParagraphTypeLabelName(){
protected function getDefaultParagraphTypeLabelName() {
if ($this->getDefaultParagraphTypeMachineName() !== NULL) {
$allowed_types = $this->getAllowedTypes();
return $allowed_types[$this->getDefaultParagraphTypeMachineName()]['label'];
Loading