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',
79 '#disabled' => !$paragraphs_type->isNew(),
82 // Loop over the plugins that can be applied to this paragraph type.
83 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
85 '#type' => 'container',
86 '#markup' => $this->t('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget.'),
87 '#attributes' => ['class' => ['messages', 'messages--warning']]
89 $form['behavior_plugins'] = [
91 '#title' => $this->t('Behaviors'),
95 $config = $paragraphs_type->get('behavior_plugins');
96 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
97 $description = $behavior_plugin_definition['description'];
98 $form['behavior_plugins'][$id]['enabled'] = [
99 '#type' => 'checkbox',
100 '#title' => $behavior_plugin_definition['label'],
101 '#title_display' => 'after',
102 '#description' => $description,
103 '#default_value' => !empty($config[$id]['enabled']),
105 $form['behavior_plugins'][$id]['settings'] = [];
106 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
107 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
108 if ($settings = $behavior_plugin->buildConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state)) {
109 $form['behavior_plugins'][$id]['settings'] = $settings + [
110 '#type' => 'fieldset',
111 '#title' => $behavior_plugin_definition['label'],
114 ':input[name="behavior_plugins[' . $id . '][enabled]"]' => ['checked' => TRUE],
128 public function validateForm(array &$form, FormStateInterface $form_state) {
129 parent::validateForm($form, $form_state);
131 $paragraphs_type = $this->entity;
133 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
134 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
135 // Only validate if the plugin is enabled and has settings.
136 if (isset($form['behavior_plugins'][$id]['settings']) && $form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
137 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
138 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
139 $behavior_plugin->validateConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
148 public function save(array $form, FormStateInterface $form_state) {
149 $paragraphs_type = $this->entity;
151 if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
152 foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
153 $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
155 // If the behavior is enabled, initialize the configuration with the
156 // enabled key and then let it process the form input.
157 if ($form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
158 $behavior_plugin->setConfiguration(['enabled' => TRUE]);
159 if (isset($form['behavior_plugins'][$id]['settings'])) {
160 $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
161 $behavior_plugin->submitConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
165 // The plugin is not enabled, reset to default configuration.
166 $behavior_plugin->setConfiguration([]);
171 $status = $paragraphs_type->save();
172 drupal_set_message($this->t('Saved the %label Paragraphs type.', array(
173 '%label' => $paragraphs_type->label(),
175 if (($status == SAVED_NEW && \Drupal::moduleHandler()->moduleExists('field_ui'))
176 && $route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type->id())) {
177 $form_state->setRedirectUrl($route_info);
180 $form_state->setRedirect('entity.paragraphs_type.collection');
187 protected function actions(array $form, FormStateInterface $form_state) {
188 $form = parent::actions($form, $form_state);
190 // We want to display the button only on add page.
191 if ($this->entity->isNew() && \Drupal::moduleHandler()->moduleExists('field_ui')) {
192 $form['submit']['#value'] = $this->t('Save and manage fields');