-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISSUE-293: Add template for Content row paragraph type Columns layout variant. #295
Open
kp77
wants to merge
2
commits into
master
Choose a base branch
from
ISSUE-293
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -710,6 +710,79 @@ function oe_theme_preprocess_paragraph__oe_content_row__variant_inpage_navigatio | |
$variables['links'] = $links; | ||
} | ||
|
||
/** | ||
* Implements hook_preprocess_paragraph() for paragraph--oe-content-row--variant-columns_layout.html.twig. | ||
* | ||
* Prepares layout grid rows and columns for displaying content | ||
* in "columns layout" variant of the "content row" paragraph type. | ||
*/ | ||
function oe_theme_preprocess_paragraph__oe_content_row__variant_columns_layout(array &$variables): void { | ||
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */ | ||
$paragraph = $variables['paragraph']; | ||
|
||
// Default number of columns in the grid. | ||
$grid_column_count = 12; | ||
|
||
// Number of columns for displaying content, default is 1. | ||
$column_count = 1; | ||
// Array of column widths (relative to the grid e.g. [4,8] or [3,6,3]). | ||
$layout = []; | ||
if (!$paragraph->get('field_oe_content_row_layout')->isEmpty()) { | ||
// Layout settings can be defined as an integer (number of equal columns), | ||
// or as a string representing the column widths in the format: '3-6-3'. | ||
$layout_settings = $paragraph->get('field_oe_content_row_layout')->value; | ||
if (is_numeric($layout_settings)) { | ||
// Set column count and layout for equal width columns. | ||
$column_count = (int) $layout_settings; | ||
$layout = array_fill(0, $column_count, $grid_column_count / $column_count); | ||
} | ||
else { | ||
// Set column count and layout for variable width columns. | ||
$layout = explode('-', $layout_settings) ?? [$grid_column_count]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require validation that we have number of columns in sum equels 12. |
||
$column_count = count($layout); | ||
} | ||
} | ||
|
||
// Create a shortcut to the render array for the field. | ||
$field_render = &$variables['content']['field_oe_paragraphs']; | ||
// Array for the template to loop through. | ||
$rows = []; | ||
// Default row array used in $rows. | ||
$row = [ | ||
'columns' => [], | ||
]; | ||
if ($column_count > 1) { | ||
// Get paragraph entities referenced for the content row paragraph. | ||
$sub_paragraphs = $paragraph->get('field_oe_paragraphs')->referencedEntities(); | ||
// Calculate number of rows or add default fallback. | ||
$row_count = $sub_paragraphs ? (int) ceil(count($sub_paragraphs) / $column_count) : 1; | ||
|
||
// Index of sub paragraph. | ||
$delta = 0; | ||
// Collect rows and columns for multi-column display. | ||
for ($i = 0; $i < $row_count; $i++) { | ||
$rows[$i] = $row; | ||
for ($j = 0; $j < $column_count; $j++) { | ||
$rows[$i]['columns'][] = [ | ||
'width' => $layout[$j], | ||
'content' => $field_render[$delta] ?? [], | ||
]; | ||
$delta++; | ||
} | ||
} | ||
} | ||
else { | ||
// Add row and column for single-column display. | ||
$row['columns'][] = [ | ||
'width' => $grid_column_count, | ||
'content' => $field_render, | ||
]; | ||
$rows[] = $row; | ||
} | ||
|
||
$variables['rows'] = $rows; | ||
} | ||
|
||
/** | ||
* Implements hook_preprocess_pattern(). | ||
*/ | ||
|
17 changes: 17 additions & 0 deletions
17
templates/paragraphs/paragraph--oe-content-row--variant-columns_layout.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{# | ||
/** | ||
* @file | ||
* Theme override to display the "content row" type paragraph in the "columns layout" variant. | ||
* | ||
* @see ./modules/contrib/paragraphs/templates/paragraph.html.twig | ||
*/ | ||
#} | ||
{% for row in rows %} | ||
<div class="ecl-row"> | ||
{% for column in row.columns %} | ||
<div class="ecl-col-md-{{ column.width }}"> | ||
{{ column.content }} | ||
</div> | ||
{% endfor %} | ||
</div> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\oe_theme\Kernel\Paragraphs; | ||
|
||
use Drupal\paragraphs\Entity\Paragraph; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
/** | ||
* Tests the "content row" layout paragraph. | ||
*/ | ||
class ContentRowLayoutTest extends ParagraphsTestBase { | ||
|
||
/** | ||
* Tests the rendering of the paragraph type "content row with 3-col layout". | ||
* | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function testRenderingThreeColLayout(): void { | ||
// Create multiple paragraphs to be referenced in the content row. | ||
$items = []; | ||
|
||
// Create a links block paragraph. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_links_block', | ||
'field_oe_text' => 'Links block title', | ||
'field_oe_links' => [ | ||
[ | ||
'title' => 'Link 1', | ||
'uri' => 'internal:/', | ||
], | ||
], | ||
]); | ||
$paragraph->save(); | ||
$items[] = $paragraph; | ||
|
||
// Create a list item paragraph. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_list_item', | ||
'oe_paragraphs_variant' => 'default', | ||
'field_oe_title' => 'List item title', | ||
'field_oe_text_long' => 'Item description', | ||
'field_oe_link' => [ | ||
'uri' => 'http://www.example.com/', | ||
], | ||
]); | ||
$paragraph->save(); | ||
$items[] = $paragraph; | ||
|
||
// Create a rich text paragraph with a title. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_rich_text', | ||
'field_oe_title' => 'Rich text title', | ||
'field_oe_text_long' => 'Rich text without title.', | ||
]); | ||
$paragraph->save(); | ||
$items[] = $paragraph; | ||
|
||
// Create the main content row paragraph with a columns layout variant. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_content_row', | ||
'oe_paragraphs_variant' => 'columns_layout', | ||
'field_oe_title' => 'Three col layout', | ||
'field_oe_content_row_layout' => '3', | ||
'field_oe_paragraphs' => $items, | ||
]); | ||
|
||
$html = $this->renderParagraph($paragraph); | ||
$crawler = new Crawler($html); | ||
|
||
// Verify the layout having three columns. | ||
$this->assertCount(3, $crawler->filter('.ecl-row .ecl-col-md-4')); | ||
|
||
// Verify that the columns contain the correct paragraph. | ||
$this->assertContains('Links block title', $crawler->filter('.ecl-row .ecl-col-md-4')->eq(0)->html()); | ||
$this->assertContains('List item title', $crawler->filter('.ecl-row .ecl-col-md-4')->eq(1)->html()); | ||
$this->assertContains('Rich text title', $crawler->filter('.ecl-row .ecl-col-md-4')->eq(2)->html()); | ||
} | ||
|
||
/** | ||
* Tests the rendering of the paragraph type "content row with 2-col layout". | ||
* | ||
* Tests the rendering of non-equal 2-col layout. | ||
* | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function testRenderingTwoNonEqualColLayout(): void { | ||
// Create multiple paragraphs to be referenced in the content row. | ||
$items = []; | ||
|
||
// Create a links block paragraph. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_links_block', | ||
'field_oe_text' => 'Links block title', | ||
'field_oe_links' => [ | ||
[ | ||
'title' => 'Link 1', | ||
'uri' => 'internal:/', | ||
], | ||
], | ||
]); | ||
$paragraph->save(); | ||
$items[] = $paragraph; | ||
|
||
// Create a list item paragraph. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_list_item', | ||
'oe_paragraphs_variant' => 'default', | ||
'field_oe_title' => 'List item title', | ||
'field_oe_text_long' => 'Item description', | ||
'field_oe_link' => [ | ||
'uri' => 'http://www.example.com/', | ||
], | ||
]); | ||
$paragraph->save(); | ||
$items[] = $paragraph; | ||
|
||
// Create the main content row paragraph with a columns layout variant. | ||
$paragraph = Paragraph::create([ | ||
'type' => 'oe_content_row', | ||
'oe_paragraphs_variant' => 'columns_layout', | ||
'field_oe_title' => 'Non-equal two col layout', | ||
'field_oe_content_row_layout' => '8-4', | ||
'field_oe_paragraphs' => $items, | ||
]); | ||
|
||
$html = $this->renderParagraph($paragraph); | ||
$crawler = new Crawler($html); | ||
|
||
// Verify the layout having two non-equal columns. | ||
$this->assertCount(1, $crawler->filter('.ecl-row .ecl-col-md-8')); | ||
$this->assertCount(1, $crawler->filter('.ecl-row .ecl-col-md-4')); | ||
|
||
// Verify that the columns contain the correct paragraph. | ||
$this->assertContains('Links block title', $crawler->filter('.ecl-row .ecl-col-md-8')->html()); | ||
$this->assertContains('List item title', $crawler->filter('.ecl-row .ecl-col-md-4')->html()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By validation before we should avoid chance to get float number