3 namespace Drupal\paragraphs\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\SubformState;
8 use Drupal\field_ui\FieldUI;
9 use Drupal\paragraphs\ParagraphsBehaviorManager;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Form controller for paragraph type forms.
15 class ParagraphsTypeForm extends EntityForm {
18 * The paragraphs behavior plugin manager service.
20 * @var \Drupal\paragraphs\ParagraphsBehaviorManager
22 protected $paragraphsBehaviorManager;
25 * The entity being used by this form.
27 * @var \Drupal\paragraphs\ParagraphsTypeInterface
32 * GeneralSettingsForm constructor.
34 * @param \Drupal\paragraphs\ParagraphsBehaviorManager $paragraphs_behavior_manager
35 * The paragraphs type feature manager service.
37 public function __construct(ParagraphsBehaviorManager $paragraphs_behavior_manager) {
38 $this->paragraphsBehaviorManager = $paragraphs_behavior_manager;
44 public static function create(ContainerInterface $container) {
46 $container->get('plugin.manager.paragraphs.behavior')
53 public function form(array $form, FormStateInterface $form_state) {
54 $form = parent::form($form, $form_state);
56 $paragraphs_type = $this->entity;
58 if (!$paragraphs_type->isNew()) {
59 $form['#title'] = (t('Edit %title paragraph type', [
60 '%title' => $paragraphs_type->label(),
64 $form['label'] = array(
65 '#type' => 'textfield',
66 '#title' => $this->t('Label'),
68 '#default_value' => $paragraphs_type->label(),
69 '#description' => $this->t("Label for the Paragraphs type."),
74 '#type' => 'machine_name',
75 '#default_value' => $paragraphs_type->id(),
76 '#machine_name' => array(
77 'exists' => 'paragraphs_type_load',
80 '#disabled' => !$paragraphs_type->isNew(),
83 $form['icon_file'] = [
84 '#title' => $this->t('Paragraph type icon'),
85 '#type' => 'managed_file',
86 '#upload_location' => 'public://paragraphs_type_icon/',
87 '#upload_validators' => [
88 'file_validate_extensions' => ['png jpg svg'],
92 if ($file = $this->entity->getIconFile()) {
93 $form['icon_file']['#default_value'] = ['target_id' => $file->id()];
96 $form['description'] = [
97 '#title' => t('Description'),
98 '#type' => 'textarea',
99 '#default_value' => $paragraphs_type->getDescription(),
100 '#description' => t('This text will be displayed on the <em>Add new paragraph</em> page.'),
103 // Loop over the plugins that can be applied to this paragraph type.
104 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
106 '#type' => 'container',
107 '#markup' => $this->t('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget.'),
108 '#attributes' => ['class' => ['messages', 'messages--warning']]
110 $form['behavior_plugins'] = [
111 '#type' => 'details',
112 '#title' => $this->t('Behaviors'),
116 $config = $paragraphs_type->get('behavior_plugins');
117 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
118 $description = $behavior_plugin_definition['description'];
119 $form['behavior_plugins'][$id]['enabled'] = [
120 '#type' => 'checkbox',
121 '#title' => $behavior_plugin_definition['label'],
122 '#title_display' => 'after',
123 '#description' => $description,
124 '#default_value' => !empty($config[$id]['enabled']),
126 $form['behavior_plugins'][$id]['settings'] = [];
127 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
128 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
129 if ($settings = $behavior_plugin->buildConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state)) {
130 $form['behavior_plugins'][$id]['settings'] = $settings + [
131 '#type' => 'fieldset',
132 '#title' => $behavior_plugin_definition['label'],
135 ':input[name="behavior_plugins[' . $id . '][enabled]"]' => ['checked' => TRUE],
149 public function validateForm(array &$form, FormStateInterface $form_state) {
150 parent::validateForm($form, $form_state);
152 $paragraphs_type = $this->entity;
154 $icon_fild = $form_state->getValue(['icon_file', '0']);
155 // Set the file UUID to the paragraph configuration.
156 if (!empty($icon_fild) && $file = $this->entityTypeManager->getStorage('file')->load($icon_fild)) {
157 $paragraphs_type->set('icon_uuid', $file->uuid());
160 $paragraphs_type->set('icon_uuid', NULL);
163 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
164 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
165 // Only validate if the plugin is enabled and has settings.
166 if (isset($form['behavior_plugins'][$id]['settings']) && $form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
167 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
168 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
169 $behavior_plugin->validateConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
178 public function save(array $form, FormStateInterface $form_state) {
179 $paragraphs_type = $this->entity;
181 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
182 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
183 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
185 // If the behavior is enabled, initialize the configuration with the
186 // enabled key and then let it process the form input.
187 if ($form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
188 $behavior_plugin->setConfiguration(['enabled' => TRUE]);
189 if (isset($form['behavior_plugins'][$id]['settings'])) {
190 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
191 $behavior_plugin->submitConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
195 // The plugin is not enabled, reset to default configuration.
196 $behavior_plugin->setConfiguration([]);
201 $status = $paragraphs_type->save();
202 drupal_set_message($this->t('Saved the %label Paragraphs type.', array(
203 '%label' => $paragraphs_type->label(),
205 if (($status == SAVED_NEW && \Drupal::moduleHandler()->moduleExists('field_ui'))
206 && $route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type->id())) {
207 $form_state->setRedirectUrl($route_info);
210 $form_state->setRedirect('entity.paragraphs_type.collection');
217 protected function actions(array $form, FormStateInterface $form_state) {
218 $form = parent::actions($form, $form_state);
220 // We want to display the button only on add page.
221 if ($this->entity->isNew() && \Drupal::moduleHandler()->moduleExists('field_ui')) {
222 $form['submit']['#value'] = $this->t('Save and manage fields');