-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mike
committed
Dec 8, 2017
0 parents
commit 0f495da
Showing
8 changed files
with
636 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 @@ | ||
This module contains a behavior plugin for the Paragraphs module. This plugin allows you to select a different view mode for the paragraph in the content add/edit form. | ||
|
||
To use this module, go to the paragraph type edit form and enable it. Select which view mode you wish to override, and which view modes you wish to allow overriding to. When adding a paragraph of this type using the experimental paragraph widget, you will have a dropdown box allowing you to select a different display mode. This can be used to control the display of a single paragraph to use different layouts or show different fields without having to create a whole new paragraph type which may collect the same fields. |
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,16 @@ | ||
{ | ||
"name": "drupal/paragraphs_viewmode", | ||
"type": "drupal-module", | ||
"description": "Allows you to select the paragraph view mode on a per paragraph basis", | ||
"keywords": ["Drupal"], | ||
"license": "GPL-2.0+", | ||
"homepage": "https://www.drupal.org/project/paragraphs_viewmode", | ||
"minimum-stability": "dev", | ||
"support": { | ||
"issues": "https://www.drupal.org/project/issues/paragraphs_viewmode", | ||
"source": "http://cgit.drupalcode.org/paragraphs_viewmode" | ||
}, | ||
"require": { | ||
"drupal/paragraphs": "^1.2" | ||
} | ||
} |
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,13 @@ | ||
paragraphs.behavior.settings.paragraphs_viewmode_behavior: | ||
type: paragraphs.behavior.settings_base | ||
label: 'Paragraphs View Mode Settings' | ||
mapping: | ||
override_mode: | ||
type: text | ||
label: 'Select which view mode to override' | ||
override_available: | ||
type: sequence | ||
label: 'Select which view modes are allowed' | ||
sequence: | ||
type: text | ||
label: 'View Mode' |
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,7 @@ | ||
name: Paragraphs View Modes | ||
type: module | ||
description: Allows you to select the paragraph view mode on a per paragraph basis | ||
core: 8.x | ||
package: Paragraphs | ||
dependencies: | ||
- paragraphs |
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,24 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains paragraph_viewmode.module. | ||
*/ | ||
|
||
use Drupal\Core\Routing\RouteMatchInterface; | ||
|
||
/** | ||
* Implements hook_help(). | ||
*/ | ||
function paragraphs_viewmode_help($route_name, RouteMatchInterface $route_match) { | ||
switch ($route_name) { | ||
// Main module help for the paragraph_viewmode module. | ||
case 'help.page.paragraph_viewmode': | ||
$output = ''; | ||
$output .= '<h3>' . t('About') . '</h3>'; | ||
$output .= '<p>' . t('Allows you to select the paragraph view mode on a per paragraph basis') . '</p>'; | ||
return $output; | ||
|
||
default: | ||
} | ||
} |
188 changes: 188 additions & 0 deletions
188
src/Plugin/paragraphs/Behavior/ParagraphsViewmodeBehavior.php
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,188 @@ | ||
<?php | ||
|
||
namespace Drupal\paragraphs_viewmode\Plugin\paragraphs\Behavior; | ||
|
||
use Drupal\Core\Entity\EntityDisplayRepositoryInterface; | ||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; | ||
use Drupal\Core\Entity\EntityFieldManager; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Render\Element; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use Drupal\paragraphs\Entity\Paragraph; | ||
use Drupal\paragraphs\ParagraphInterface; | ||
use Drupal\paragraphs\ParagraphsBehaviorBase; | ||
use Drupal\paragraphs\ParagraphsBehaviorInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Class ParagraphsViewmodeBehavior. | ||
* | ||
* @ParagraphsBehavior( | ||
* id="paragraphs_viewmode_behavior", | ||
* label=@Translation("Paragraphs View Mode"), | ||
* description=@Translation("A Plugin to allow overriding a paragraph view mode while on default") | ||
* ) | ||
*/ | ||
class ParagraphsViewmodeBehavior extends ParagraphsBehaviorBase implements ParagraphsBehaviorInterface { | ||
use StringTranslationTrait; | ||
|
||
/** | ||
* The entity Display Repository. | ||
* | ||
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface | ||
*/ | ||
protected $entityDisplayRepository; | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
/** | ||
* Constructs a ParagraphsViewmodeBehavior object. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing information about the plugin instance. | ||
* @param string $plugin_id | ||
* The plugin_id for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager | ||
* The entity field manager. | ||
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository | ||
* The entity display repository. | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager); | ||
|
||
$this->entityTypeManager = $entity_type_manager; | ||
$this->entityDisplayRepository = $entity_display_repository; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static($configuration, $plugin_id, $plugin_definition, | ||
$container->get('entity_field.manager'), | ||
$container->get('entity_display.repository'), | ||
$container->get('entity_type.manager') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) { | ||
$override_mode = $this->configuration['override_mode']; | ||
$new_view_mode = $paragraph->getBehaviorSetting($this->pluginId, 'view_mode', $override_mode); | ||
|
||
if ($view_mode != $override_mode || $override_mode == $new_view_mode) { | ||
return $build; | ||
} | ||
|
||
$viewBuilder = $this->entityTypeManager->getViewBuilder($paragraph->getEntityTypeId()); | ||
$elements = $viewBuilder->view($paragraph, $new_view_mode); | ||
|
||
if (isset($elements['#pre_render'])) { | ||
foreach ($elements['#pre_render'] as $callable) { | ||
if (is_string($callable) && strpos($callable, '::') === FALSE) { | ||
$callable = $this->controllerResolver->getControllerFromDefinition($callable); | ||
} | ||
$elements = call_user_func($callable, $elements); | ||
} | ||
} | ||
|
||
$build['#view_mode'] = $new_view_mode; | ||
foreach (Element::children($build) as $key) { | ||
unset($build[$key]); | ||
} | ||
foreach (Element::children($elements) as $key) { | ||
$build[$key] = $elements[$key]; | ||
} | ||
|
||
return $build; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | ||
$view_modes = $this->entityDisplayRepository->getViewModeOptions('paragraph'); | ||
|
||
$form['override_mode'] = [ | ||
'#type' => 'select', | ||
'#title' => $this->t('Select which view mode to override'), | ||
'#options' => $view_modes, | ||
'#default_value' => $this->configuration['override_mode'], | ||
]; | ||
$form['override_available'] = [ | ||
'#type' => 'select', | ||
'#title' => $this->t('Select which view modes are allowable'), | ||
'#multiple' => TRUE, | ||
'#options' => $view_modes, | ||
'#default_value' => $this->configuration['override_available'], | ||
]; | ||
|
||
return parent::buildConfigurationForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | ||
$override_mode = $form_state->getValue('override_mode'); | ||
$override_available = $form_state->getValue('override_available'); | ||
|
||
/* Require the original mode in the allowed list */ | ||
if (!in_array($override_mode, $override_available)) { | ||
$override_available[$override_mode] = $override_mode; | ||
} | ||
|
||
$this->configuration['override_mode'] = $override_mode; | ||
$this->configuration['override_available'] = $override_available; | ||
|
||
// TODO: Change the autogenerated stub. | ||
parent::submitConfigurationForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return parent::defaultConfiguration() + [ | ||
'override_mode' => 'default', | ||
'override_available' => [], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) { | ||
$all_modes = $this->entityDisplayRepository->getViewModeOptions('paragraph'); | ||
$mode = $paragraph->getBehaviorSetting($this->pluginId, 'view_mode', $this->configuration['override_mode']); | ||
$mode_options = array_intersect_key($all_modes, $this->configuration['override_available']); | ||
$form['view_mode'] = [ | ||
'#type' => 'select', | ||
'#title' => 'Select which view mode to use for this paragraph', | ||
'#options' => $mode_options, | ||
'#default_value' => $mode, | ||
]; | ||
return parent::buildBehaviorForm($paragraph, $form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) { | ||
$paragraph->setBehaviorSettings($this->pluginId, ['view_mode', $form_state->getValue('view_mode')]); | ||
parent::submitBehaviorForm($paragraph, $form, $form_state); | ||
} | ||
|
||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\paragraphs_viewmode\Functional; | ||
|
||
use Drupal\Core\Url; | ||
use Drupal\Tests\BrowserTestBase; | ||
|
||
/** | ||
* Simple test to ensure that main page loads with module enabled. | ||
* | ||
* @group paragraph_viewmode | ||
*/ | ||
class LoadTest extends BrowserTestBase { | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
public static $modules = ['paragraph_viewmode']; | ||
|
||
/** | ||
* A user with permission to administer site configuration. | ||
* | ||
* @var \Drupal\user\UserInterface | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() { | ||
parent::setUp(); | ||
$this->user = $this->drupalCreateUser(['administer site configuration']); | ||
$this->drupalLogin($this->user); | ||
} | ||
|
||
/** | ||
* Tests that the home page loads with a 200 response. | ||
*/ | ||
public function testLoad() { | ||
$this->drupalGet(Url::fromRoute('<front>')); | ||
$this->assertResponse(200); | ||
} | ||
|
||
} |