Skip to content
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

D8CORE-7212 Add behavior to teaser paragraph to change headers to h3 #294

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
{# Card Super Headline #}
{%- block block_card_super_headline %}
{% if card_super_headline is not empty %}
<span>{{ card_super_headline }}</span>
<div>{{ card_super_headline }}</div>
{% endif -%}
{% endblock -%}

Expand Down
61 changes: 61 additions & 0 deletions modules/jumpstart_ui/jumpstart_ui.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @file
* jumpstart_ui.install
*/

/**
* Update list paragraphs that do not have headline fields.
*/
function jumpstart_ui_update_9001(&$sandbox) {
$paragraph_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
if (empty($sandbox['ids'])) {
$sandbox['ids'] = $paragraph_storage->getQuery()
->accessCheck(FALSE)
->condition('type', 'stanford_lists')
->condition('su_list_headline', '', 'IS NULL')
->condition('su_list_view', '', '<>')
->execute();
$sandbox['total'] = count($sandbox['ids']);
}
$ids = array_splice($sandbox['ids'], 0, 25);

/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
foreach ($paragraph_storage->loadMultiple($ids) as $paragraph) {
$behaviors = $paragraph->getAllBehaviorSettings();
$behaviors['list_paragraph']['hide_heading'] = TRUE;
$paragraph->setBehaviorSettings('list_paragraph', $behaviors['list_paragraph']);

$view = $paragraph->get('su_list_view')->get(0)->getValue();
$new_header = 'Site content';

switch ($view['target_id']) {
case 'stanford_basic_pages':
$new_header = 'Site Pages';
break;

case 'stanford_courses':
$new_header = 'Courses';
break;

case 'stanford_event_series':
$new_header = 'Event Series';
break;

case 'stanford_events':
$new_header = 'Events';
break;

case 'stanford_news':
$new_header = 'Site news';
break;

case 'stanford_person':
$new_header = 'People';
break;
}
$paragraph->set('su_list_headline', $new_header)->save();
}
$sandbox['#finished'] = count($sandbox['ids']) ? 1 - count($sandbox['ids']) / $sandbox['total'] : 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, F
'#description' => $this->t('This message will appear for site visitors if there are no items displayed in the list.'),
'#default_value' => $paragraph->getBehaviorSetting('list_paragraph', 'empty_message'),
];
$form['hide_heading'] = [
'#type' => 'checkbox',
'#title' => $this->t('Visually Hide Heading'),
'#default_value' => $paragraph->getBehaviorSetting('list_paragraph', 'hide_heading', FALSE),
];
return $form;
}

/**
* {@inheritDoc}
*/
public function view(array &$build, ParagraphInterface $paragraph, EntityViewDisplayInterface $display, $view_mode) {
// Visually hide the header.
if ($paragraph->getBehaviorSetting('list_paragraph', 'hide_heading', FALSE) && isset($build['su_list_headline'][0])) {
$build['su_list_headline']['#attributes']['class'][] = 'visually-hidden';
}

if (!isset($build['su_list_view']) || !empty(Element::children($build['su_list_view']))) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Drupal\jumpstart_ui\Plugin\paragraphs\Behavior;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\paragraphs\ParagraphsTypeInterface;

/**
* Class HeroPatternBehavior.
*
* @ParagraphsBehavior(
* id = "stanford_teaser",
* label = @Translation("Teaser Paragraph"),
* description = @Translation("Display options for the Teaser paragraph.")
* )
*/
class TeaserParagraphBehavior extends ParagraphsBehaviorBase {

/**
* {@inheritDoc}
*/
public static function isApplicable(ParagraphsTypeInterface $paragraphs_type) {
return $paragraphs_type->id() == 'stanford_entity';
}

/**
* {@inheritDoc}
*/
public function defaultConfiguration() {
return ['hide_heading' => FALSE];
}

/**
* {@inheritDoc}
*/
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$form = parent::buildBehaviorForm($paragraph, $form, $form_state);
$form['hide_heading'] = [
'#type' => 'checkbox',
'#title' => $this->t('Visually Hide Heading'),
'#default_value' => $paragraph->getBehaviorSetting('stanford_teaser', 'hide_heading', FALSE),
];
return $form;
}

/**
* {@inheritDoc}
*/
public function view(array &$build, ParagraphInterface $paragraph, EntityViewDisplayInterface $display, $view_mode) {
if (!isset($build['su_entity_headline'][0]) || !isset($build['su_entity_item'][0])) {
return;
}
if ($paragraph->getBehaviorSetting('stanford_teaser', 'hide_heading', FALSE)) {
$build['su_entity_headline']['#attributes']['class'][] = 'visually-hidden';
}
foreach (Element::children($build['su_entity_item']) as $delta) {
$build['su_entity_item'][$delta]['#view_mode'] = 'stanford_h3_card';

// Replace the cache keys to match the view mode.
$cache_key = array_search('stanford_card', $build['su_entity_item'][$delta]['#cache']['keys']);
$build['su_entity_item'][$delta]['#cache']['keys'][$cache_key] = 'stanford_h3_card';
}
}

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

namespace Drupal\Tests\jumpstart_ui\Unit\Plugin\paragraphs\Behavior;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Form\FormState;
use Drupal\jumpstart_ui\Plugin\paragraphs\Behavior\TeaserParagraphBehavior;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\Tests\UnitTestCase;

/**
* Class TeaserParagraphBehaviorTest
*
* @group jumpstart_ui
* @coversDefaultClass \Drupal\jumpstart_ui\Plugin\paragraphs\Behavior\TeaserParagraphBehavior
*/
class TeaserParagraphBehaviorTest extends UnitTestCase {

/**
* {@inheritDoc}
*/
public function setup(): void {
parent::setUp();

$field_manager = $this->createMock(EntityFieldManagerInterface::class);

$container = new ContainerBuilder();
$container->set('entity_field.manager', $field_manager);
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container);
}

/**
* The paragraph behavior should only be available to hero pattern displays.
*/
public function testApplication() {
$paragraph_type = $this->createMock(ParagraphsType::class);
$paragraph_type->method('id')->willReturn('foo');
$this->assertFalse(TeaserParagraphBehavior::isApplicable($paragraph_type));

$paragraph_type = $this->createMock(ParagraphsType::class);
$paragraph_type->method('id')->willReturn('stanford_entity');
$this->assertTrue(TeaserParagraphBehavior::isApplicable($paragraph_type));
}

public function testForm() {
$plugin = TeaserParagraphBehavior::create(\Drupal::getContainer(), [], '', []);
$paragraph = $this->createMock(ParagraphInterface::class);
$paragraph->method('getBehaviorSetting')->willReturn(FALSE);
$form = [];
$form_state = new FormState();
$form = $plugin->buildBehaviorForm($paragraph, $form, $form_state);
$this->assertArrayHasKey('hide_heading', $form);
$this->assertFalse($form['hide_heading']['#default_value']);
}

public function testView() {
$plugin = TeaserParagraphBehavior::create(\Drupal::getContainer(), [], '', []);
$build = ['su_entity_headline' => ['foo']];
$paragraph = $this->createMock(Paragraph::class);
$paragraph->method('getBehaviorSetting')->willReturn(TRUE);
$display = $this->createMock(EntityViewDisplayInterface::class);
$plugin->view($build, $paragraph, $display, 'foo');
// No changes with no items.
$this->assertEquals(['su_entity_headline' => ['foo']], $build);

$build['su_entity_item'][0] = [
'#view_mode' => 'foobar',
'#cache' => ['keys' => ['foobar']]
];
$plugin->view($build, $paragraph, $display, 'foo');
$this->assertContains('visually-hidden', $build['su_entity_headline']['#attributes']['class']);
$this->assertContains('stanford_h3_card', $build['su_entity_item'][0]['#cache']['keys']);
}

/**
* Load and get mock display entities.
*
* @param array $ids
* Array of display ids.
*
* @return array
* Keyed array of mock displays.
*/
public function loadMultipleDisplayCallback($ids = []) {
$return = [];
foreach ($ids as $id) {
$ds_settings = NULL;
switch ($id) {
case 'paragraph.foo.hero':
$ds_settings = ['id' => 'pattern_hero'];
break;
}
$return[$id] = $this->createMock(EntityDisplayInterface::class);
$return[$id]->method('getThirdPartySetting')
->willReturn($ds_settings);
}
return $return;
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@
}

.su-card__contents {
@include modular-spacing("padding", 1);
padding: 3rem;

h2 {
@include type-c;
@include grid-media-max("sm") {
@include modular-spacing("margin-top", 1);
}
}

h3 {
font-size: 1.4em;

margin-left: 0;
margin-right: 0;
@include grid-media-max("sm") {
@include modular-spacing("margin-top", 1);
}
}

a {
Expand Down
2 changes: 1 addition & 1 deletion modules/stanford_courses/dist/css/course-list-item.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading