diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index c40afea6667c7080231348ccfaaa0648a8dfa534..3f91ace2aa4be86cae077b865ef8308deb03df43 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -130,7 +130,7 @@ class ParagraphsWidget extends WidgetBase { */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityFieldManagerInterface $entity_field_manager) { // Modify settings that were set before https://www.drupal.org/node/2896115. - if(isset($settings['edit_mode']) && $settings['edit_mode'] === 'preview') { + if (isset($settings['edit_mode']) && $settings['edit_mode'] === 'preview') { $settings['edit_mode'] = 'closed'; $settings['closed_mode'] = 'preview'; } @@ -158,7 +158,7 @@ class ParagraphsWidget extends WidgetBase { * {@inheritdoc} */ public static function defaultSettings() { - return array( + return [ 'title' => t('Paragraph'), 'title_plural' => t('Paragraphs'), 'edit_mode' => 'open', @@ -168,40 +168,43 @@ class ParagraphsWidget extends WidgetBase { 'add_mode' => 'dropdown', 'form_display_mode' => 'default', 'default_paragraph_type' => '', - 'features' => ['duplicate' => 'duplicate', 'collapse_edit_all' => 'collapse_edit_all'], - ); + 'features' => [ + 'duplicate' => 'duplicate', + 'collapse_edit_all' => 'collapse_edit_all', + ], + ]; } /** * {@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.'), '#options' => $this->getSettingOptions('edit_mode'), '#default_value' => $this->getSetting('edit_mode'), '#required' => TRUE, - ); + ]; $elements['closed_mode'] = [ '#type' => 'select', @@ -221,7 +224,7 @@ class ParagraphsWidget extends WidgetBase { '#required' => TRUE, '#states' => [ 'visible' => [ - 'select[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][edit_mode]"]' => ['value' => 'closed'], + 'select[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][edit_mode]"]' => ['value' => 'closed'], ], ], ]; @@ -234,30 +237,30 @@ class ParagraphsWidget extends WidgetBase { '#min' => 0, '#states' => [ 'invisible' => [ - 'select[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][edit_mode]"]' => ['value' => 'open'], + 'select[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][edit_mode]"]' => ['value' => 'open'], ], ], ]; - $elements['add_mode'] = array( + $elements['add_mode'] = [ '#type' => 'select', '#title' => $this->t('Add mode'), '#description' => $this->t('The way to add new Paragraphs.'), '#options' => $this->getSettingOptions('add_mode'), '#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')), '#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']; } @@ -295,13 +298,13 @@ class ParagraphsWidget extends WidgetBase { * - "edit_mode" * - "closed_mode" * - "autocollapse" - * - "add_mode" + * - "add_mode". * * @return array|null * An array of setting option usable as a value for a "#options" key. */ protected function getSettingOptions($setting_name) { - switch($setting_name) { + switch ($setting_name) { case 'edit_mode': $options = [ 'open' => $this->t('Open'), @@ -309,18 +312,21 @@ class ParagraphsWidget extends WidgetBase { 'closed_expand_nested' => $this->t('Closed, show nested'), ]; break; + case 'closed_mode': $options = [ 'summary' => $this->t('Summary'), 'preview' => $this->t('Preview'), ]; break; + case 'autocollapse': $options = [ 'none' => $this->t('None'), 'all' => $this->t('All'), ]; break; + case 'add_mode': $options = [ 'select' => $this->t('Select list'), @@ -329,6 +335,7 @@ class ParagraphsWidget extends WidgetBase { 'modal' => $this->t('Modal form'), ]; break; + case 'features': $options = [ 'duplicate' => $this->t('Duplicate'), @@ -349,10 +356,10 @@ class ParagraphsWidget 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'), ]); $edit_mode = $this->getSettingOptions('edit_mode')[$this->getSetting('edit_mode')]; @@ -371,11 +378,11 @@ class ParagraphsWidget extends WidgetBase { $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(), ]); } $features_labels = array_intersect_key($this->getSettingOptions('features'), array_filter($this->getSetting('features'))); @@ -446,9 +453,9 @@ class ParagraphsWidget 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($host, $field_name); $item_mode = 'edit'; @@ -543,18 +550,18 @@ class ParagraphsWidget 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>'; @@ -632,7 +639,9 @@ class ParagraphsWidget extends WidgetBase { '#name' => $id_prefix . '_duplicate', '#weight' => 502, '#submit' => [[get_class($this), 'duplicateSubmit']], - '#limit_validation_errors' => [array_merge($parents, [$field_name, $delta])], + '#limit_validation_errors' => [ + array_merge($parents, [$field_name, $delta]) + ], '#delta' => $delta, '#ajax' => [ 'callback' => [get_class($this), 'itemAjax'], @@ -683,7 +692,7 @@ class ParagraphsWidget extends WidgetBase { '#limit_validation_errors' => [], '#delta' => $delta, '#ajax' => [ - 'callback' => array(get_class($this), 'itemAjax'), + 'callback' => [get_class($this), 'itemAjax'], 'wrapper' => $widget_state['ajax_wrapper_id'], ], '#access' => $this->removeButtonAccess($paragraphs_entity), @@ -701,7 +710,9 @@ class ParagraphsWidget extends WidgetBase { '#name' => $id_prefix . '_collapse', '#weight' => 1, '#submit' => [[get_class($this), 'paragraphsItemSubmit']], - '#limit_validation_errors' => [array_merge($parents, [$field_name, $delta])], + '#limit_validation_errors' => [ + array_merge($parents, [$field_name, $delta]) + ], '#delta' => $delta, '#ajax' => [ 'callback' => [get_class($this), 'itemAjax'], @@ -711,7 +722,11 @@ class ParagraphsWidget extends WidgetBase { '#paragraphs_mode' => 'closed', '#paragraphs_show_warning' => TRUE, '#attributes' => [ - 'class' => ['paragraphs-icon-button', 'paragraphs-icon-button-collapse', 'button--extrasmall'], + 'class' => [ + 'paragraphs-icon-button', + 'paragraphs-icon-button-collapse', + 'button--extrasmall' + ], 'title' => $this->t('Collapse'), ], ]; @@ -736,7 +751,11 @@ class ParagraphsWidget extends WidgetBase { '#access' => $paragraphs_entity->access('update') && !$translating_force_close, '#paragraphs_mode' => 'edit', '#attributes' => [ - 'class' => ['paragraphs-icon-button', 'paragraphs-icon-button-edit', 'button--extrasmall'], + 'class' => [ + 'paragraphs-icon-button', + 'paragraphs-icon-button-edit', + 'button--extrasmall' + ], 'title' => $this->t('Edit'), ], ]); @@ -804,13 +823,19 @@ class ParagraphsWidget extends WidgetBase { if (!empty($widget_actions['actions'])) { // Expand all actions to proper submit elements and add it to top // actions sub component. - $element['top']['actions']['actions'] = array_map([$this, 'expandButton'], $widget_actions['actions']); + $element['top']['actions']['actions'] = array_map( + [$this, 'expandButton'], + $widget_actions['actions'] + ); } if (!empty($widget_actions['dropdown_actions'])) { // Expand all dropdown actions to proper submit elements and add // them to top dropdown actions sub component. - $element['top']['actions']['dropdown_actions'] = array_map([$this, 'expandButton'], $widget_actions['dropdown_actions']); + $element['top']['actions']['dropdown_actions'] = array_map( + [$this, 'expandButton'], + $widget_actions['dropdown_actions'] + ); } } @@ -829,7 +854,10 @@ class ParagraphsWidget 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'; @@ -856,14 +884,15 @@ class ParagraphsWidget extends WidgetBase { foreach (Element::children($element['subform']) as $field) { if ($paragraphs_entity->hasField($field)) { - $field_definition = $paragraphs_entity->get($field)->getFieldDefinition(); + $field_definition = $paragraphs_entity->get($field) + ->getFieldDefinition(); // 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. @@ -891,7 +920,7 @@ class ParagraphsWidget extends WidgetBase { else { $element['subform'][$field]['widget']['#after_build'][] = [ static::class, - 'addTranslatabilityClue' + 'addTranslatabilityClue', ]; } } @@ -907,7 +936,10 @@ class ParagraphsWidget extends WidgetBase { $element['behavior_plugins'][$plugin_id] = [ '#type' => 'container', '#group' => implode('][', array_merge($element_parents, ['paragraph_behavior'])), - '#parents' => array_merge(array_slice($element_parents, 0, -1), ['behavior_plugins', $plugin_id]), + '#parents' => array_merge( + array_slice($element_parents, 0, -1), + ['behavior_plugins', $plugin_id] + ), ]; $subform_state = SubformState::createForSubform($element['behavior_plugins'][$plugin_id], $form, $form_state); if ($plugin_form = $plugin->buildBehaviorForm($paragraphs_entity, $element['behavior_plugins'][$plugin_id], $subform_state)) { @@ -1026,7 +1058,7 @@ class ParagraphsWidget extends WidgetBase { $element['behavior_plugins'] = []; } else { - $element['subform'] = array(); + $element['subform'] = []; } // If we have any info items lets add them to the top section. @@ -1242,17 +1274,18 @@ class ParagraphsWidget extends WidgetBase { * Returns the sorted allowed types for a entity reference field. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * (optional) The field definition forwhich the allowed types should be - * returned, defaults to the current field. + * (optional) The field definition forwhich the allowed types should be + * returned, defaults to the current 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(?FieldDefinitionInterface $field_definition = NULL) { - $return_bundles = array(); + $return_bundles = []; /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager */ $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection'); $handler = $selection_manager->getSelectionHandler($field_definition ?: $this->fieldDefinition); @@ -1267,10 +1300,10 @@ class ParagraphsWidget extends WidgetBase { 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++; } @@ -1312,7 +1345,7 @@ class ParagraphsWidget extends WidgetBase { 'entity' => $paragraphs_entity, 'display' => $display, 'mode' => 'edit', - 'original_delta' => 1 + 'original_delta' => 1, ]; $max = 1; $field_state['items_count'] = $max; @@ -1325,9 +1358,9 @@ class ParagraphsWidget extends WidgetBase { $field_title = $this->fieldDefinition->getLabel(); $description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription() ?? '')); - $elements = array(); + $elements = []; $tabs = ''; - $this->fieldIdPrefix = implode('-', array_merge($this->fieldParents, array($field_name))); + $this->fieldIdPrefix = implode('-', array_merge($this->fieldParents, [$field_name])); $this->fieldWrapperId = Html::getId($this->fieldIdPrefix . '-add-more-wrapper'); // If the parent entity is paragraph add the nested class if not then add @@ -1368,7 +1401,6 @@ class ParagraphsWidget extends WidgetBase { ]); $elements['#attached']['library'][] = 'paragraphs/paragraphs-dragdrop'; - //$elements['dragdrop_mode']['#button_type'] = 'primary'; $elements['dragdrop'] = $this->buildNestedParagraphsFoDragDrop($form_state, NULL, []); return $elements; } @@ -1381,12 +1413,12 @@ class ParagraphsWidget 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 ? '' : $field_title, '#description' => $is_multiple ? '' : $description, - ); + ]; $element = $this->formSingleElement($items, $delta, $element, $form, $form_state); if ($element) { @@ -1394,15 +1426,15 @@ class ParagraphsWidget 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. '#delta' => $max, '#default_value' => $items[$delta]->_weight ?: $delta, '#weight' => 100, - ); + ]; } // Access for the top element is set to FALSE only when the paragraph @@ -1479,7 +1511,7 @@ class ParagraphsWidget extends WidgetBase { '#attributes' => [ 'class' => [ 'paragraph-type-add-delta', - $this->getSetting('add_mode') + $this->getSetting('add_mode'), ], ], ]; @@ -1640,13 +1672,13 @@ class ParagraphsWidget extends WidgetBase { // We name the element '_weight' to avoid clashing with elements // defined by widget. - $element['_weight'] = array( + $element['_weight'] = [ '#type' => 'hidden', '#default_value' => $child_delta, '#attributes' => [ 'class' => ['paragraphs-dragdrop__weight'], - ] - ); + ], + ]; $element['_path'] = [ '#type' => 'hidden', @@ -1655,14 +1687,20 @@ class ParagraphsWidget extends WidgetBase { '#default_value' => $child_path, '#attributes' => [ 'class' => ['paragraphs-dragdrop__path'], - ] + ], ]; $summary_options = []; $element['#prefix'] = '<li class="paragraphs-dragdrop__item" data-paragraphs-dragdrop-bundle="' . $child_paragraph->bundle() . '"><a href="#" class="paragraphs-dragdrop__handle"><span class="paragraphs-dragdrop__icon"></span></a>'; $element['#suffix'] = '</li>'; - $child_array_parents = array_merge($array_parents, [$child_field_name, $child_delta]); + $child_array_parents = array_merge( + $array_parents, + [ + $child_field_name, + $child_delta + ] + ); if ($child_elements = $this->buildNestedParagraphsFoDragDrop($form_state, $child_paragraph, $child_array_parents)) { $element['dragdrop'] = $child_elements; @@ -1694,7 +1732,7 @@ class ParagraphsWidget 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) { @@ -1808,7 +1846,7 @@ class ParagraphsWidget extends WidgetBase { * * @param array $form * Form array. - * @param FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state object. * @param int $position * Position of triggering element. @@ -1896,7 +1934,14 @@ class ParagraphsWidget extends WidgetBase { '#name' => $this->fieldIdPrefix . '_' . $machine_name . '_add_more', '#value' => $add_mode == 'modal' ? $label : $this->t('Add @type', ['@type' => $label]), '#attributes' => ['class' => ['field-add-more-submit', 'button--small']], - '#limit_validation_errors' => [array_merge($this->fieldParents, [$this->fieldDefinition->getName(), 'add_more'])], + '#limit_validation_errors' => [ + array_merge( + $this->fieldParents, [ + $this->fieldDefinition->getName(), + 'add_more' + ] + ) + ], '#submit' => [[get_class($this), 'addMoreSubmit']], '#ajax' => [ 'callback' => [get_class($this), 'addMoreAjax'], @@ -1970,7 +2015,14 @@ class ParagraphsWidget 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'], @@ -1979,7 +2031,7 @@ class ParagraphsWidget extends WidgetBase { ], ]; - $add_more_elements['add_more_button']['#suffix'] = $this->t(' to %type', ['%type' => $field_title]); + $add_more_elements['add_more_button']['#suffix'] = $this->t('to %type', ['%type' => $field_title]); return $add_more_elements; } @@ -2204,6 +2256,9 @@ class ParagraphsWidget extends WidgetBase { $form_state->setRebuild(); } + /** + * {@inheritdoc} + */ public static function paragraphsItemSubmit(array $form, FormStateInterface $form_state) { $submit = ParagraphsWidget::getSubmitElementInfo($form, $form_state, ParagraphsWidget::ACTION_POSITION_ACTIONS); @@ -2259,6 +2314,9 @@ class ParagraphsWidget extends WidgetBase { return $submit['element']; } + /** + * {@inheritdoc} + */ public static function itemAjax(array $form, FormStateInterface $form_state) { $submit = ParagraphsWidget::getSubmitElementInfo($form, $form_state, ParagraphsWidget::ACTION_POSITION_ACTIONS); @@ -2296,13 +2354,12 @@ class ParagraphsWidget extends WidgetBase { $form_state->setRebuild(); } - /** * Reorder paragraphs. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. - * @param $field_values_parents + * @param mixed $field_values_parents * The field value parents. */ protected static function reorderParagraphs(FormStateInterface $form_state, $field_values_parents) { @@ -2311,7 +2368,7 @@ class ParagraphsWidget extends WidgetBase { $complete_field_storage = NestedArray::getValue( $form_state->getStorage(), [ 'field_storage', - '#parents' + '#parents', ] ); $new_field_storage = $complete_field_storage; @@ -2357,7 +2414,7 @@ class ParagraphsWidget extends WidgetBase { '#fields', $field_name, 'paragraphs', - $delta + $delta, ] ); $path = explode('][', $item_values['_path']); @@ -2374,7 +2431,7 @@ class ParagraphsWidget extends WidgetBase { '#fields', $new_field_name, 'paragraphs', - $item_values['_weight'] + $item_values['_weight'], ] ); $key_exists = NULL; @@ -2406,13 +2463,27 @@ class ParagraphsWidget extends WidgetBase { // it to an empty array in case all paragraphs have been moved away // from it. foreach (array_keys($item_values['dragdrop']) as $sub_field_name) { - $new_widget_state_keys = array_merge($parents, [$field_name, $item_values['_weight'] ,'subform', '#fields', $sub_field_name]); + $new_widget_state_keys = array_merge( + $parents, [ + $field_name, + $item_values['_weight'] , + 'subform', '#fields', + $sub_field_name + ] + ); if (!NestedArray::getValue($new_field_storage, $new_widget_state_keys)) { NestedArray::setValue($new_field_storage, $new_widget_state_keys, ['paragraphs' => []]); } } - $reorder_paragraphs($item_values['dragdrop'], array_merge($parents, [$field_name, $delta, 'subform']), $item_state['entity']); + $reorder_paragraphs($item_values['dragdrop'], array_merge( + $parents, [ + $field_name, + $delta, + 'subform' + ] + ), + $item_state['entity']); } } } @@ -2575,7 +2646,6 @@ class ParagraphsWidget extends WidgetBase { return $element; } - /** * Special handling to validate form elements with multiple values. * @@ -2607,7 +2677,7 @@ class ParagraphsWidget extends WidgetBase { $element = NestedArray::getValue($form_state->getCompleteForm(), $widget_state['array_parents'] ?? []); if (!empty($widget_state['dragdrop'])) { - $path = array_merge($form['#parents'], array($field_name)); + $path = array_merge($form['#parents'], [$field_name]); static::reorderParagraphs($form_state, $path); // After re-ordering, get the updated widget state. @@ -2703,7 +2773,7 @@ class ParagraphsWidget 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? + // @todo Maybe we should actually delete it here? elseif (isset($widget_state['paragraphs'][$item['_original_delta']]['mode']) && $widget_state['paragraphs'][$item['_original_delta']]['mode'] == 'remove') { $item['target_id'] = NULL; $item['target_revision_id'] = NULL; @@ -2721,7 +2791,7 @@ class ParagraphsWidget extends WidgetBase { // Remove buttons from header actions. $field_name = $this->fieldDefinition->getName(); - $path = array_merge($form['#parents'], array($field_name)); + $path = array_merge($form['#parents'], [$field_name]); $form_state_variables = $form_state->getValues(); $key_exists = NULL; $values = NestedArray::getValue($form_state_variables, $path, $key_exists); @@ -2742,7 +2812,9 @@ class ParagraphsWidget extends WidgetBase { * Initializes $this->isTranslating. * * @param \Drupal\Core\Form\FormStateInterface $form_state + * String. * @param \Drupal\Core\Entity\ContentEntityInterface $host + * String. */ protected function initIsTranslating(FormStateInterface $form_state, ContentEntityInterface $host) { if ($this->isTranslating != NULL) { @@ -2760,7 +2832,8 @@ class ParagraphsWidget extends WidgetBase { return; } - // Supporting \Drupal\content_translation\Controller\ContentTranslationController. + // Supporting + // \Drupal\content_translation\Controller\ContentTranslationController. if (!empty($form_state->get('content_translation'))) { // Adding a translation. $this->isTranslating = TRUE; @@ -2779,9 +2852,12 @@ class ParagraphsWidget extends WidgetBase { * "(all languages)" suffix to the widget title, replicate that here. * * @param array $element + * String. * @param \Drupal\Core\Form\FormStateInterface $form_state + * String. * * @return array + * The container form element */ public static function addTranslatabilityClue(array $element, FormStateInterface $form_state) { static $suffix, $fapi_title_elements; @@ -2792,7 +2868,23 @@ class ParagraphsWidget 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 @@ -2932,7 +3024,7 @@ class ParagraphsWidget extends WidgetBase { '#limit_validation_errors' => [ array_merge($this->fieldParents, [$field_name, 'dragdrop_mode']), ], - '#access' => $this->allowReferenceChanges() + '#access' => $this->allowReferenceChanges(), ]); } } @@ -2976,7 +3068,11 @@ class ParagraphsWidget extends WidgetBase { // order and with the right settings. if ($mode === 'closed') { $edit_all['#attributes'] = [ - 'class' => ['paragraphs-icon-button', 'paragraphs-icon-button-edit', 'button--extrasmall'], + 'class' => [ + 'paragraphs-icon-button', + 'paragraphs-icon-button-edit', + 'button--extrasmall' + ], 'title' => $this->t('Edit all'), ]; $edit_all['#title'] = $this->t('Edit All'); @@ -2985,7 +3081,11 @@ class ParagraphsWidget extends WidgetBase { } else { $collapse_all['#attributes'] = [ - 'class' => ['paragraphs-icon-button', 'paragraphs-icon-button-collapse', 'button--extrasmall'], + 'class' => [ + 'paragraphs-icon-button', + 'paragraphs-icon-button-collapse', + 'button--extrasmall' + ], 'title' => $this->t('Collapse all'), ]; $actions['actions']['collapse_all'] = $collapse_all; @@ -3199,7 +3299,10 @@ class ParagraphsWidget extends WidgetBase { $form_state->setError($element[$item['_original_delta']], $new_message); } else { - $form_state->setError($element[$item['_original_delta']], $this->t('Validation error on collapsed paragraph @path: @message', ['@path' => $violation->getPropertyPath(), '@message' => $violation->getMessage()])); + $form_state->setError($element[$item['_original_delta']], $this->t('Validation error on collapsed paragraph @path: @message', [ + '@path' => $violation->getPropertyPath(), + '@message' => $violation->getMessage() + ])); } }