Skip to content

Commit

Permalink
Issue #2661446 by slashrsm, Primsi: Use FieldWidgetDisplay plugins to…
Browse files Browse the repository at this point in the history
… display entities in Multi step selection display
  • Loading branch information
slashrsm committed Mar 2, 2016
1 parent a3faf0b commit bfc7fc2
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 44 deletions.
24 changes: 24 additions & 0 deletions css/entity_browser.entity_list.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file
* Styles for lists of entities display using FieldWidgetDisplayInterface plugins.
*
* Currently used on Entity browser entity reference field widget and Multi-step
* selection display plugin.
*
* These styles are compatible with Bartik, Seven, and Classy themes.
*/

.entities-list .item-container {
display: inline-block;
margin: 10px 10px 15px;
vertical-align: text-bottom;
}

.entities-list .item-container > img, .entities-list .item-container > input {
display: block;
margin: 5px auto 0;
}

.entities-list .rendered-entity {
display: block;
}
24 changes: 0 additions & 24 deletions css/entity_browser.entity_reference.css

This file was deleted.

11 changes: 8 additions & 3 deletions entity_browser.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ common:
dependencies:
- core/underscore

entity_list:
version: VERSION
css:
component:
css/entity_browser.entity_list.css: {}

iframe:
version: VERSION
js:
Expand All @@ -22,13 +28,11 @@ iframe_selection:

entity_reference:
version: VERSION
css:
component:
css/entity_browser.entity_reference.css: {}
js:
js/entity_browser.entity_reference.js: {}
dependencies:
- entity_browser/common
- entity_browser/entity_list
- core/jquery.ui.sortable

modal:
Expand Down Expand Up @@ -67,4 +71,5 @@ multi_step_display:
js:
js/entity_browser.multi_step_display.js: {}
dependencies:
- entity_browser/entity_list
- core/jquery.ui.sortable
4 changes: 2 additions & 2 deletions js/entity_browser.entity_reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Defines the behavior of the entity reference widget that utilizes entity
* browser.
*/
(function ($, Drupal, drupalSettings) {
(function ($, Drupal) {

"use strict";
/**
Expand Down Expand Up @@ -40,4 +40,4 @@
$(this).parent().parent().find('input[type*=hidden]').val(ids.join(' '));
};

}(jQuery, Drupal, drupalSettings));
}(jQuery, Drupal));
12 changes: 5 additions & 7 deletions js/entity_browser.multi_step_display.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file entity_browser.multi_step_display.js
*
*/
(function ($, Drupal, drupalSettings) {
(function ($, Drupal) {

"use strict";

Expand All @@ -11,10 +11,8 @@
*/
Drupal.behaviors.entityBrowserMultiStepDisplay = {
attach: function (context) {
$(context).find('.entity-browser-form').each(function () {
$(this).find('.selected-entities-list').sortable({
stop: Drupal.entityBrowserMultiStepDisplay.entitiesReordered
});
$(context).find('.entities-list').sortable({
stop: Drupal.entityBrowserMultiStepDisplay.entitiesReordered
});
}
};
Expand All @@ -30,10 +28,10 @@
* Object with detailed information about the sort event.
*/
Drupal.entityBrowserMultiStepDisplay.entitiesReordered = function(event, ui) {
var items = $(this).find('.selected-item-container');
var items = $(this).find('.item-container');
for (var i = 0; i < items.length; i++) {
$(items[i]).find('.weight').val(i);
}
};

}(jQuery, Drupal, drupalSettings));
}(jQuery, Drupal));
145 changes: 138 additions & 7 deletions src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

/**
* Contains \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay.
*/

namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay;

use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_browser\FieldWidgetDisplayManager;
use Drupal\entity_browser\SelectionDisplayBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Show current selection and delivers selected entities.
Expand All @@ -20,6 +21,58 @@
*/
class MultiStepDisplay extends SelectionDisplayBase {

/**
* Field widget display plugin manager.
*
* @var \Drupal\entity_browser\FieldWidgetDisplayManager
*/
protected $fieldDisplayManager;

/**
* Constructs widget plugin.
*
* @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 \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher service.
* @param \Drupal\Core\Entity\EntityManagerInterface
* Entity manager service.
* @param \Drupal\entity_browser\FieldWidgetDisplayManager $field_display_manager
* Field widget display plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, FieldWidgetDisplayManager $field_display_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_manager);
$this->fieldDisplayManager = $field_display_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('event_dispatcher'),
$container->get('entity.manager'),
$container->get('plugin.manager.entity_browser.field_widget_display')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'entity_type' => 'node',
'display' => 'label',
'display_settings' => [],
] + parent::defaultConfiguration();
}

/**
* {@inheritdoc}
*/
Expand All @@ -30,17 +83,26 @@ public function getForm(array &$original_form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'entity_browser/multi_step_display';
$form['selected'] = [
'#theme_wrappers' => ['container'],
'#attributes' => ['class' => ['selected-entities-list']],
'#attributes' => ['class' => ['entities-list']],
'#tree' => TRUE
];
foreach ($selected_entities as $id => $entity) {
$display_plugin = $this->fieldDisplayManager->createInstance(
$this->configuration['display'],
$this->configuration['display_settings'] + ['entity_type' => $this->configuration['entity_type']]
);
$display = $display_plugin->view($entity);
if (is_string($display)) {
$display = ['#markup' => $display];
}

$form['selected']['items_'. $entity->id()] = [
'#theme_wrappers' => ['container'],
'#attributes' => [
'class' => ['selected-item-container'],
'class' => ['item-container'],
'data-entity-id' => $entity->id()
],
'display' => ['#markup' => $entity->label()],
'display' => $display,
'remove_button' => [
'#type' => 'submit',
'#value' => $this->t('Remove'),
Expand Down Expand Up @@ -123,4 +185,73 @@ public static function saveNewOrder(FormStateInterface $form_state) {
}
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$default_entity_type = $form_state->getValue('entity_type', $this->configuration['entity_type']);
$default_display = $form_state->getValue('display', $this->configuration['display']);
$default_display_settings = $form_state->getValue('display_settings', $this->configuration['display_settings']);
$default_display_settings += ['entity_type' => $default_entity_type];

$form['#prefix'] = '<div id="multi-step-form-wrapper">';
$form['#suffix'] = '</div>';

$entity_types = [];
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
$entity_types[$entity_type_id] = $entity_type->getLabel();
}
$form['entity_type'] = [
'#type' => 'select',
'#title' => $this->t('Entity type'),
'#description' => $this->t("Entity browser itself does not need information about entity type being selected. It can actually select entities of different type. However, some of the display plugins need to know which entity type they are operating with. Display plugins that do not need this info will ignore this configuration value."),
'#default_value' => $default_entity_type,
'#options' => $entity_types,
'#ajax' => [
'callback' => [$this, 'updateSettingsAjax'],
'wrapper' => 'multi-step-form-wrapper',
],
];

$displays = [];
foreach ($this->fieldDisplayManager->getDefinitions() as $display_plugin_id => $definition) {
$entity_type = $this->entityManager->getDefinition($default_entity_type);
if ($this->fieldDisplayManager->createInstance($display_plugin_id)->isApplicable($entity_type)) {
$displays[$display_plugin_id] = $definition['label'];
}
}
$form['display'] = [
'#title' => t('Entity display plugin'),
'#type' => 'select',
'#default_value' => $default_display,
'#options' => $displays,
'#ajax' => [
'callback' => [$this, 'updateSettingsAjax'],
'wrapper' => 'multi-step-form-wrapper',
],
];

$form['display_settings'] = [
'#type' => 'container',
'#title' => t('Entity display plugin configuration'),
'#tree' => TRUE,
];
if ($default_display_settings) {
$display_plugin = $this->fieldDisplayManager
->createInstance($default_display, $default_display_settings);

$form['display_settings'] += $display_plugin->settingsForm($form, $form_state);
}

return $form;
}

/**
* Ajax callback that updates multi-step plugin configuration form on AJAX updates.
*/
public function updateSettingsAjax(array $form, FormStateInterface $form_state) {
return $form;
}

}
3 changes: 2 additions & 1 deletion src/SelectionDisplayBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->eventDispatcher = $event_dispatcher;
$this->entityManager = $entity_manager;
$this->setConfiguration($configuration);
}

/**
Expand Down Expand Up @@ -97,7 +98,7 @@ public function getConfiguration() {
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
$this->configuration = $configuration + $this->defaultConfiguration();
}

/**
Expand Down

0 comments on commit bfc7fc2

Please sign in to comment.