Skip to content

Commit

Permalink
Merge pull request #290 from UN-OCHA/berliner/HPC-9945
Browse files Browse the repository at this point in the history
HPC-9945: Add settings for promotable and full width to multi column interactive content
  • Loading branch information
berliner authored Dec 12, 2024
2 parents f767f3f + 9c10299 commit 10d4560
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,74 @@ langcode: en
status: true
dependencies:
config:
- field.field.paragraph.interactive_content_2_columns.field_full_width
- paragraphs.paragraphs_type.interactive_content_2_columns
module:
- field_group
third_party_settings:
field_group:
group_tabs:
children:
- group_layout
- group_settings
label: Tabs
region: content
parent_name: ''
weight: 0
format_type: tabs
format_settings:
classes: ''
show_empty_fields: false
id: ''
label_as_html: false
direction: horizontal
width_breakpoint: 640
group_layout:
children:
- layout_paragraphs_fields
label: Layout
region: content
parent_name: group_tabs
weight: 4
format_type: tab
format_settings:
classes: ''
show_empty_fields: true
id: ''
label_as_html: false
formatter: closed
description: ''
required_fields: true
group_settings:
children:
- field_full_width
label: 'Additional settings'
region: content
parent_name: group_tabs
weight: 5
format_type: tab
format_settings:
classes: ''
show_empty_fields: false
id: ''
label_as_html: false
formatter: closed
description: ''
required_fields: true
id: paragraph.interactive_content_2_columns.default
targetEntityType: paragraph
bundle: interactive_content_2_columns
mode: default
content:
field_full_width:
type: boolean_checkbox
weight: 3
region: content
settings:
display_label: true
third_party_settings: { }
layout_paragraphs_fields:
weight: 0
weight: 2
region: content
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
uuid: 4ccfb4b9-f82a-4e70-ad8a-953d448c90e2
langcode: en
status: true
dependencies:
config:
- field.field.paragraph.interactive_content_2_columns.field_full_width
- paragraphs.paragraphs_type.interactive_content_2_columns
id: paragraph.interactive_content_2_columns.default
targetEntityType: paragraph
bundle: interactive_content_2_columns
mode: default
content: { }
hidden:
field_full_width: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
uuid: 6e0f7d23-ee57-41e0-8d62-f08316679e46
langcode: en
status: true
dependencies:
config:
- field.storage.paragraph.field_full_width
- paragraphs.paragraphs_type.interactive_content_2_columns
module:
- field_config_cardinality
third_party_settings:
field_config_cardinality:
cardinality_config: '1'
cardinality_label_config: false
unlimited_not_required: ''
limited_not_required: ''
limited_required: ''
id: paragraph.interactive_content_2_columns.field_full_width
field_name: field_full_width
entity_type: paragraph
bundle: interactive_content_2_columns
label: 'Display at full width?'
description: 'If checked, the interactive content will be shown at full-width. Otherwise it will be restricted to same width as the surrounding text content.'
required: false
translatable: false
default_value:
-
value: 0
default_value_callback: ''
settings:
on_label: 'Yes, full width'
off_label: 'No, align with content'
field_type: boolean
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ behavior_plugins:
available_layouts:
layout_twocol_interactive: 'Two column (interactive content)'
layout_threecol_interactive: 'Three column (interactive content)'
promoted_behavior:
enabled: true
10 changes: 9 additions & 1 deletion html/modules/custom/gho_layouts/src/LayoutSelectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ class LayoutSelectHelper {
public static function processLayoutSelect(array &$element, FormStateInterface $form_state, array &$form) {
$element['#attached']['library'][] = 'gho_layouts/layout_select';

// Make the details wrapper a container, so it shows directly.
$form['layout_paragraphs']['config']['#type'] = 'container';
// Hide the "Administrative label input, we don't need that.
$form['layout_paragraphs']['config']['label']['#access'] = FALSE;

if (empty($form['#region_component_restrict'])) {
// The rest here applies only to elements where some layout options
// should be restricted.
return $element;
}

Expand All @@ -43,6 +50,7 @@ public static function processLayoutSelect(array &$element, FormStateInterface $
$element[$layout]['#disabled'] = TRUE;
}
}

if (!empty($disabled)) {
$form['layout_paragraphs']['disabled_message'] = [
'#type' => 'html_tag',
Expand All @@ -58,7 +66,7 @@ public static function processLayoutSelect(array &$element, FormStateInterface $
}

/**
* Get th layout plugin manager.
* Get the layout plugin manager.
*
* @return \Drupal\Core\Layout\LayoutPluginManagerInterface
* The layout plugin manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\layout_builder\Plugin\Layout\MultiWidthLayoutBase;

/**
Expand All @@ -19,12 +20,23 @@ abstract class MultiColumnLayoutBase extends MultiWidthLayoutBase {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$input = $form_state->getUserInput();

$element_parents = ['layout_paragraphs', 'config', 'column_widths'];
$input = $form_state->getUserInput();
if ($form_state instanceof SubformStateInterface) {
$input = $form_state->getCompleteFormState()->getUserInput();
}
$columns_width = NestedArray::getValue($input, $element_parents);
if (!array_key_exists($columns_width, $this->getWidthOptions())) {
NestedArray::setValue($input, $element_parents, $this->getDefaultWidth());
$form_state->setUserInput($input);
$columns_width = $this->getDefaultWidth();
NestedArray::setValue($input, $element_parents, $columns_width);
if ($form_state instanceof SubformStateInterface) {
$form_state->getCompleteFormState()->setUserInput($input);
}
else {
$form_state->setUserInput($input);
}

}
return $form;
}
Expand Down
49 changes: 8 additions & 41 deletions html/modules/custom/ncms_paragraphs/ncms_paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Form\FormStateInterface;
use Drupal\ncms_paragraphs\Entity\NcmsParagraphGeneric;
use Drupal\ncms_paragraphs\Entity\NcmsParagraphInterface;
use Drupal\ncms_paragraphs\Helpers\StringHelper;

Expand All @@ -15,61 +16,27 @@ use Drupal\ncms_paragraphs\Helpers\StringHelper;
function ncms_paragraphs_entity_bundle_info_alter(array &$bundles) {
foreach (array_keys($bundles['paragraph'] ?? []) as $paragraph_type) {
$class_name = '\\Drupal\ncms_paragraphs\\Entity\\Paragraph\\' . StringHelper::makeCamelCase($paragraph_type, FALSE);
if (!class_exists($class_name)) {
continue;
if (class_exists($class_name)) {
$bundles['paragraph'][$paragraph_type]['class'] = $class_name;
$bundles['paragraph'][$paragraph_type]['label'] = ucfirst(str_replace('_', ' ', $paragraph_type));
}
else {
$bundles['paragraph'][$paragraph_type]['class'] = NcmsParagraphGeneric::class;
}
$bundles['paragraph'][$paragraph_type]['class'] = $class_name;
$bundles['paragraph'][$paragraph_type]['label'] = ucfirst(str_replace('_', ' ', $paragraph_type));
}
}

/**
* Implements hook_form_FORM_ID_alter().
*/
function ncms_paragraphs_form_layout_paragraphs_component_form_alter(&$form, FormStateInterface $form_state) {
function ncms_paragraphs_form_layout_paragraphs_component_form_alter(&$form, FormStateInterface &$form_state) {
/** @var \Drupal\layout_paragraphs\Form\EditComponentForm $form_object */
$form_object = $form_state->getFormObject();
$paragraph = $form_object->getParagraph();

if ($paragraph instanceof NcmsParagraphInterface) {
$paragraph->entityFormAlter($form, $form_state);
}

// Don't trigger "Required" messages when canceling the add/edit form.
$form['actions']['cancel']['#limit_validation_errors'] = [];

// Add our library to improve the display.
$form['#attached']['library'][] = 'ncms_paragraphs/paragraph_edit_form';

// Move the behavior plugin from a stand-alone fieldset into existing field
// groups.
if (!empty($form['#fieldgroups']) && !empty($form['behavior_plugins'])) {

$form['behavior_plugins']['#parents'] = ['behavior_plugins'];
$form['behavior_plugins'] = $form_object->behaviorPluginsForm($form['behavior_plugins'], $form_state, $form);
unset($form['behavior_plugins']['#process']);

$group_name = 'group_settings';
if (empty($form['#fieldgroups'][$group_name])) {
$group = clone end($form['#fieldgroups']);
$group->children = ['behavior_plugins'];
$group->format_settings['formatter'] = 'close';
$group->weight = $group->weight + 1;
$group->group_name = $group_name;
$form['#fieldgroups'][$group->group_name] = $group;
}
else {
$form['#fieldgroups'][$group_name]->children[] = 'behavior_plugins';
}
$form['#fieldgroups'][$group_name]->label = t('Additional settings');
$form['#group_children']['behavior_plugins'] = $group_name;
}

if (!empty($form['field_dataset'])) {
// Hide the fieldset.
$form['field_dataset']['widget'][0]['#type'] = 'container';
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,56 @@ abstract class NcmsParagraphBase extends Paragraph implements NcmsParagraphInter
/**
* {@inheritdoc}
*/
public function entityFormAlter(&$form, FormStateInterface $form_state) {}
public function entityFormAlter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\layout_paragraphs\Form\EditComponentForm $form_object */
$form_object = $form_state->getFormObject();
// Move the behavior plugin from a stand-alone fieldset into existing field
// groups.
if (!empty($form['#fieldgroups']) && !empty($form['behavior_plugins'])) {
$form['behavior_plugins']['#parents'] = ['behavior_plugins'];
$form['behavior_plugins'] = $form_object->behaviorPluginsForm($form['behavior_plugins'], $form_state, $form);
unset($form['behavior_plugins']['#process']);
$group_name = 'group_settings';
if (empty($form['#fieldgroups'][$group_name])) {
$group = clone end($form['#fieldgroups']);
$group->children = ['behavior_plugins'];
$group->format_settings['formatter'] = 'close';
$group->weight = $group->weight + 1;
$group->group_name = $group_name;
$form['#fieldgroups'][$group->group_name] = $group;
}
else {
$form['#fieldgroups'][$group_name]->children[] = 'behavior_plugins';
}
$form['#fieldgroups'][$group_name]->label = $this->t('Additional settings');
$form['#group_children']['behavior_plugins'] = $group_name;
}

// Don't trigger "Required" messages when canceling the add/edit form.
$form['actions']['cancel']['#limit_validation_errors'] = [];

// Add our library to improve the display.
$form['#attached']['library'][] = 'ncms_paragraphs/paragraph_edit_form';

if (!empty($form['field_dataset'])) {
// Hide the fieldset.
$form['field_dataset']['widget'][0]['#type'] = 'container';
}
}

/**
* {@inheritdoc}
*/
public function preprocess(&$variables) {}

/**
* {@inheritdoc}
*/
public function isFullWidth() {
if (!$this->hasField('field_full_width')) {
return FALSE;
}
return $this->get('field_full_width')->value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drupal\ncms_paragraphs\Entity;

/**
* Entity class for paragraphs of type interactive_content.
*/
class NcmsParagraphGeneric extends NcmsParagraphBase {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function entityFormAlter(&$form, FormStateInterface $form_state);
*/
public function preprocess(&$variables);

/**
* Whether the paragraph is a full width paragraph.
*/
public function isFullWidth();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class InteractiveContent extends NcmsParagraphBase {
* {@inheritdoc}
*/
public function entityFormAlter(&$form, FormStateInterface $form_state) {
parent::entityFormAlter($form, $form_state);

if (!empty($form['field_embed_code'])) {
$form['field_embed_code']['#element_validate'][] = [
$this,
Expand Down Expand Up @@ -41,4 +43,16 @@ public function validateEmbedCodeField($element, FormStateInterface $form_state)
}
}

/**
* {@inheritdoc}
*/
public function isFullWidth() {
if (!empty($this->getBehaviorSetting('layout_paragraphs', 'parent_uuid'))) {
// Interactive content widgets that are embedded inside other paragraphs
// should never display in full width.
return FALSE;
}
return parent::isFullWidth();
}

}
Loading

0 comments on commit 10d4560

Please sign in to comment.