Skip to content

Instantly share code, notes, and snippets.

@colepacak
Created January 23, 2018 18:18
Show Gist options
  • Save colepacak/7f6bfd834c16f603e071d10c433eb55b to your computer and use it in GitHub Desktop.
Save colepacak/7f6bfd834c16f603e071d10c433eb55b to your computer and use it in GitHub Desktop.
diff --git a/config/install/image.style.paragraph_summary.yml b/config/install/image.style.paragraph_summary.yml
new file mode 100644
index 0000000..7fc6a6f
--- /dev/null
+++ b/config/install/image.style.paragraph_summary.yml
@@ -0,0 +1,12 @@
+langcode: en
+status: true
+name: paragraph_summary
+label: 'Paragraph Summary'
+effects:
+ paragraph_summary_scale_and_crop:
+ uuid: paragraph_summary_scale_and_crop
+ id: image_scale_and_crop
+ weight: 1
+ data:
+ width: 30px
+ height: 20px
\ No newline at end of file
diff --git a/css/paragraphs.widget.css b/css/paragraphs.widget.css
index 12780b3..cbd5b4f 100644
--- a/css/paragraphs.widget.css
+++ b/css/paragraphs.widget.css
@@ -132,8 +132,8 @@
.js .paragraph-type-top {
display: -ms-grid;
display: grid;
- -ms-grid-columns: 100px auto 1fr auto;
- grid-template-columns: 100px auto 1fr auto;
+ -ms-grid-columns: 250px auto 1fr auto;
+ grid-template-columns: 250px auto 1fr auto;
-ms-grid-rows: auto;
grid-template-rows: auto;
grid-gap: 5px 5px;
@@ -145,26 +145,27 @@
.js .paragraph-type-top .paragraph-type-title {
grid-column: 1 / 2;
- -ms-grid-row-span: 1;
+ -ms-grid-column: 1;
-ms-grid-row: 1;
- grid-row: 1 / span 1;
+ -ms-grid-row-span: 1;
+ grid-row: 1 / span 1;
}
.js .paragraph-type-top .paragraph-type-info {
grid-column: 2 / 3;
-ms-grid-column: 2;
- -ms-grid-row-span: 1;
-ms-grid-row: 1;
- grid-row: 1 / span 1;
+ -ms-grid-row-span: 1;
+ grid-row: 1 / span 1;
}
.js .paragraph-type-top .paragraph-type-summary {
grid-column: 1 / 5;
-ms-grid-column: 1;
-ms-grid-column-span: 5;
- -ms-grid-row-span: 1;
-ms-grid-row: 2;
- grid-row: 2 / span 1;
+ -ms-grid-row-span: 1;
+ grid-row: 2 / span 1;
}
@media (min-width: 600px) {
@@ -172,9 +173,9 @@
grid-column: 3 / 4;
-ms-grid-column: 3;
-ms-grid-column-span: 1;
- -ms-grid-row-span: 1;
-ms-grid-row: 1;
- grid-row: 1 / span 1;
+ -ms-grid-row-span: 1;
+ grid-row: 1 / span 1;
}
}
@@ -182,9 +183,9 @@
grid-column: 3 / 5;
-ms-grid-column: 3;
-ms-grid-column-span: 2;
- -ms-grid-row-span: 1;
-ms-grid-row: 1;
- grid-row: 1 / span 1;
+ -ms-grid-row-span: 1;
+ grid-row: 1 / span 1;
justify-self: end;
-ms-grid-column-align: end;
}
diff --git a/css/paragraphs.widget.scss b/css/paragraphs.widget.scss
index 609bef9..7376c1b 100644
--- a/css/paragraphs.widget.scss
+++ b/css/paragraphs.widget.scss
@@ -124,7 +124,7 @@
.paragraph-type-top {
display: grid;
- grid-template-columns: 100px auto 1fr auto;
+ grid-template-columns: 250px auto 1fr auto;
grid-template-rows: auto;
grid-gap: $gutter-top $gutter-top;
align-items: center;
diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php
index 378216c..4ba3326 100644
--- a/src/Entity/Paragraph.php
+++ b/src/Entity/Paragraph.php
@@ -16,6 +16,8 @@ use Drupal\entity_reference_revisions\EntityNeedsSaveInterface;
use Drupal\entity_reference_revisions\EntityNeedsSaveTrait;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\user\UserInterface;
+use Drupal\file\Entity\File;
+use Drupal\image\Entity\ImageStyle;
/**
* Defines the Paragraph entity.
@@ -484,6 +486,99 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
return strip_tags($collapsed_summary_text);
}
+ public function getExtendedSummary(array $options = [], $form_display_info) {
+ $show_behavior_summary = isset($options['show_behavior_summary']) ? $options['show_behavior_summary'] : TRUE;
+ $depth_limit = isset($options['depth_limit']) ? $options['depth_limit'] : 1;
+ $this->summaryCount = 0;
+ $summary = [];
+ // Load the form display for the paragraph type so we can get field weights.
+ $form_display_weights = \Drupal::entityTypeManager()
+ ->getStorage('entity_form_display')
+ ->load($form_display_info['entity_type'] . '.' . $form_display_info['bundle'] . '.' . $form_display_info['mode'])->get('content');
+
+ $field_definitions = $this->getFieldDefinitions();
+
+ // For each field definition, compare to the form display weights and add to the field definition for later comparison.
+ foreach($field_definitions as $field_definition) {
+ $field_name = $field_definition->getName();
+ if(array_key_exists($field_name, $form_display_weights)) {
+ $field_definition->field_weight = $form_display_weights[$field_name]['weight'];
+ }
+ }
+
+ // Sort the field definitions by field weight, if set.
+ uasort($field_definitions, [$this, 'cmp']);
+
+ foreach ($field_definitions as $field_name => $field_definition) {
+ if (in_array($field_definition->getType(), ['file', 'video'])) {
+ $file_summary = $this->getFileSummary($field_name);
+ if ($file_summary != '') {
+ $summary[] = $file_summary;
+ }
+ }
+
+ if ($field_definition->getType() == 'image') {
+ $style = ImageStyle::load('paragraph_summary');
+ $fid = $this->$field_name->getValue()[0]['target_id'];
+ // Fid will be null if collapsing a paragraph that hasn't been filled out for example.
+ if($fid !== null) {
+ $image_path = File::load($fid)->getFileUri();
+
+ $summary[] = '<img class="paragraph-extended-summary-image" src="' . $style->buildUrl($image_path) . '">';
+ }
+ }
+
+ $text_summary = $this->getTextSummary($field_name, $field_definition);
+ if ($text_summary != '') {
+ $summary[] = strip_tags($text_summary);
+ }
+
+ if ($field_definition->getType() == 'entity_reference_revisions') {
+ // Decrease the depth, since we are entering a nested paragraph.
+ $nested_summary = $this->getNestedSummary($field_name, [
+ 'show_behavior_summary' => $show_behavior_summary,
+ 'depth_limit' => $depth_limit - 1
+ ]);
+ if ($nested_summary != '') {
+ $summary[] = strip_tags($nested_summary);
+ }
+ }
+
+ if ($field_type = $field_definition->getType() == 'entity_reference') {
+ if (!in_array($field_name, ['type', 'uid', 'revision_uid'])) {
+ if ($this->get($field_name)->entity) {
+ $summary[] = strip_tags($this->get($field_name)->entity->label());
+ }
+ }
+ }
+
+ // Add the Block admin label referenced by block_field.
+ if ($field_definition->getType() == 'block_field') {
+ if (!empty($this->get($field_name)->first())) {
+ $block_admin_label = $this->get($field_name)->first()->getBlock()->getPluginDefinition()['admin_label'];
+ $summary[] = strip_tags($block_admin_label);
+ }
+ }
+ }
+
+ if ($show_behavior_summary) {
+ $paragraphs_type = $this->getParagraphType();
+ foreach ($paragraphs_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin) {
+ if ($plugin_summary = $plugin->settingsSummary($this)) {
+ $summary = array_merge($summary, $plugin_summary);
+ }
+ }
+ }
+
+ if ($this->summaryCount) {
+ array_unshift($summary, (string) \Drupal::translation()->formatPlural($this->summaryCount, '1 child', '@count children'));
+ }
+
+ $collapsed_summary_text = implode(' — ', $summary);
+ return $collapsed_summary_text;
+ }
+
+
/**
* Returns an array of field names to skip in ::isChanged.
*
@@ -644,12 +739,14 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
'text_long',
'list_string',
'string',
+ 'string_long'
];
$excluded_text_types = [
'parent_id',
'parent_type',
'parent_field_name',
+ 'behavior_settings'
];
$summary = '';
@@ -669,4 +766,19 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
return trim($summary);
}
+ protected function cmp($a, $b) {
+ if (!isset($a->field_weight) && !isset($b->field_weight)) {
+ return 0;
+ }
+ else if(!isset($a->field_weight)) {
+ return -1;
+ }
+ else if(!isset($b->field_weight)) {
+ return 1;
+ }
+ else {
+ return ($a->field_weight < $b->field_weight) ? -1 : 1;
+ }
+ }
+
}
diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
index 84b47ea..eeb0598 100644
--- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
+++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
@@ -242,6 +242,7 @@ class ParagraphsWidget extends WidgetBase {
case 'closed_mode':
$options = [
'summary' => $this->t('Summary'),
+ 'extended_summary' => $this->t('Extended Summary'),
'preview' => $this->t('Preview'),
];
break;
@@ -743,6 +744,23 @@ class ParagraphsWidget extends WidgetBase {
$element['preview'] = $view_builder->view($paragraphs_entity, 'preview', $paragraphs_entity->language()->getId());
$element['preview']['#access'] = $paragraphs_entity->access('view');
}
+ else if($closed_mode_setting === 'extended_summary') {
+ // The closed paragraph is displayed as an extended summary.
+ $form_info = [
+ 'entity_type' => $context['entity_type'],
+ 'bundle' => $context['bundle'],
+ 'mode' => $context['mode']
+ ];
+ $summary = $paragraphs_entity->getExtendedSummary([], $form_info);
+ if (!empty($summary)) {
+ $element['top']['summary']['fields_info'] = [
+ '#markup' => $summary,
+ '#prefix' => '<div class="paragraphs-collapsed-description extended-summary">',
+ '#suffix' => '</div>',
+ '#access' => $paragraphs_entity->access('view'),
+ ];
+ }
+ }
else {
// The closed paragraph is displayed as a summary.
if ($paragraphs_entity) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment