public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$paragraphs_type = $this->entity;
if (!$paragraphs_type
->isNew()) {
$form['#title'] = $this
->t('Edit %title paragraph type', [
'%title' => $paragraphs_type
->label(),
]);
}
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $paragraphs_type
->label(),
'#description' => $this
->t("Label for the Paragraphs type."),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $paragraphs_type
->id(),
'#machine_name' => array(
'exists' => 'paragraphs_type_load',
),
'#maxlength' => 32,
'#disabled' => !$paragraphs_type
->isNew(),
);
$form['icon_file'] = [
'#title' => $this
->t('Paragraph type icon'),
'#type' => 'managed_file',
'#upload_location' => ParagraphsTypeInterface::ICON_UPLOAD_LOCATION,
'#upload_validators' => [
'file_validate_extensions' => [
'png jpg svg',
],
],
];
if ($file = $this->entity
->getIconFile()) {
$form['icon_file']['#default_value'] = [
'target_id' => $file
->id(),
];
}
$form['description'] = [
'#title' => $this
->t('Description'),
'#type' => 'textarea',
'#default_value' => $paragraphs_type
->getDescription(),
'#description' => $this
->t('This text will be displayed on the <em>Add new paragraph</em> page.'),
];
// Loop over the plugins that can be applied to this paragraph type.
if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager
->getApplicableDefinitions($paragraphs_type)) {
$form['message'] = [
'#type' => 'container',
'#markup' => $this
->t('Behavior plugins are only supported by the stable paragraphs widget.', [], [
'context' => 'paragraphs',
]),
'#attributes' => [
'class' => [
'messages',
'messages--warning',
],
],
];
$form['behavior_plugins'] = [
'#type' => 'details',
'#title' => $this
->t('Behaviors', [], [
'context' => 'paragraphs',
]),
'#tree' => TRUE,
'#open' => TRUE,
];
$config = $paragraphs_type
->get('behavior_plugins');
// Alphabetically sort plugins by plugin label.
uasort($behavior_plugin_definitions, function ($a, $b) {
return strcmp($a['label'], $b['label']);
});
foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
$description = $behavior_plugin_definition['description'];
$form['behavior_plugins'][$id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $behavior_plugin_definition['label'],
'#title_display' => 'after',
'#description' => $description,
'#default_value' => !empty($config[$id]['enabled']),
];
$form['behavior_plugins'][$id]['settings'] = [];
$subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
$behavior_plugin = $paragraphs_type
->getBehaviorPlugin($id);
if ($settings = $behavior_plugin
->buildConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state)) {
$form['behavior_plugins'][$id]['settings'] = $settings + [
'#type' => 'fieldset',
'#title' => $behavior_plugin_definition['label'],
'#states' => [
'visible' => [
':input[name="behavior_plugins[' . $id . '][enabled]"]' => [
'checked' => TRUE,
],
],
],
];
}
}
}
return $form;
}