View source
<?php
namespace Drupal\paragraphs_features;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\paragraphs_features\Ajax\ScrollToElementCommand;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget;
class ParagraphsFeatures {
public static $availableFeatures = [
'add_in_between',
'delete_confirmation',
'split_text',
];
public static function getWrapperId(array $parents, $field_name) {
return Html::getId(implode('-', array_merge($parents, [
$field_name,
])) . '-add-more-wrapper');
}
public static function registerFormWidgetFeatures(array &$elements, ParagraphsWidget $widget, $fieldWrapperId) {
foreach (static::$availableFeatures as $feature) {
if ($widget
->getThirdPartySetting('paragraphs_features', $feature)) {
$elements['add_more']['#attached']['library'][] = 'paragraphs_features/drupal.paragraphs_features.' . $feature;
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature][$fieldWrapperId] = TRUE;
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature]['_path'] = drupal_get_path('module', 'paragraphs_features');
}
}
$elements['add_more']['#attached']['library'][] = 'paragraphs_features/drupal.paragraphs_features.scroll_to_element';
foreach (Element::children($elements['add_more']) as $button) {
$elements['add_more'][$button]['#ajax']['callback'] = [
static::class,
'addMoreAjax',
];
}
if (!empty($elements['header_actions']['dropdown_actions']['dragdrop_mode'])) {
$elements['header_actions']['dropdown_actions']['dragdrop_mode']['#access'] = (bool) $widget
->getThirdPartySetting('paragraphs_features', 'show_drag_and_drop', TRUE);
}
}
public static function addMoreAjax(array $form, FormStateInterface $form_state) {
$element = ParagraphsWidget::addMoreAjax($form, $form_state);
$response = new AjaxResponse();
$response
->addCommand(new InsertCommand(NULL, $element));
$response
->addCommand(new ScrollToElementCommand($element[$element['#max_delta']]['#attributes']['data-drupal-selector'], $element['#attributes']['data-drupal-selector']));
return $response;
}
public static function getThirdPartyForm(WidgetInterface $plugin, $field_name) {
$elements = [];
$elements['delete_confirmation'] = [
'#type' => 'checkbox',
'#title' => t('Enable confirmation on paragraphs remove'),
'#default_value' => $plugin
->getThirdPartySetting('paragraphs_features', 'delete_confirmation'),
'#attributes' => [
'class' => [
'paragraphs-features__delete-confirmation__option',
],
],
];
$modal_related_options_rule = [
':input[name="fields[' . $field_name . '][settings_edit_form][settings][add_mode]"]' => [
'value' => 'modal',
],
];
$elements['add_in_between'] = [
'#type' => 'checkbox',
'#title' => t('Enable add in between buttons'),
'#default_value' => $plugin
->getThirdPartySetting('paragraphs_features', 'add_in_between'),
'#attributes' => [
'class' => [
'paragraphs-features__add-in-between__option',
],
],
'#states' => [
'enabled' => $modal_related_options_rule,
'visible' => $modal_related_options_rule,
],
];
$elements['split_text'] = [
'#type' => 'checkbox',
'#title' => t('Enable split text for text paragraphs'),
'#default_value' => $plugin
->getThirdPartySetting('paragraphs_features', 'split_text'),
'#attributes' => [
'class' => [
'paragraphs-features__split-text__option',
],
],
'#states' => [
'enabled' => $modal_related_options_rule,
'visible' => $modal_related_options_rule,
],
];
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery
->getLibraryByName('paragraphs', 'paragraphs-dragdrop');
$elements['show_drag_and_drop'] = [
'#type' => 'checkbox',
'#title' => t('Show drag & drop button'),
'#default_value' => $plugin
->getThirdPartySetting('paragraphs_features', 'show_drag_and_drop', TRUE),
'#access' => !empty($library),
];
return $elements;
}
}