Commit 9cd28855 authored by alexpott's avatar alexpott

Issue #2029321 by tim.plunkett, aspilicious, swentel: Provide list and form...

Issue #2029321 by tim.plunkett, aspilicious, swentel: Provide list and form controllers for EntityViewMode and EntityFormMode.
parent d03acd3c
......@@ -10,6 +10,102 @@
use Drupal\Core\Config\Entity\ConfigStorageController;
/**
* Implements hook_permission().
*/
function entity_permission() {
return array(
'administer display modes' => array(
'title' => t('Add, edit, and delete custom display modes.'),
),
);
}
/**
* Implements hook_menu().
*/
function entity_menu() {
$items = array();
$items['admin/structure/display-modes'] = array(
'title' => 'Display modes',
'description' => 'Configure what displays are available for your content and forms.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer display modes'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
// View modes.
$items['admin/structure/display-modes/view'] = array(
'title' => 'View modes',
'description' => 'Manage custom view modes.',
'route_name' => 'entity_view_mode.list',
);
$items['admin/structure/display-modes/view/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/display-modes/view/add'] = array(
'title' => 'Add view mode',
'route_name' => 'entity_view_mode.add',
'type' => MENU_SIBLING_LOCAL_TASK,
);
$items['admin/structure/display-modes/view/add/%'] = array(
'route_name' => 'entity_view_mode.add_type',
);
$items['admin/structure/display-modes/view/manage/%'] = array(
'title' => 'Edit view mode',
'route_name' => 'entity_view_mode.edit',
);
$items['admin/structure/display-modes/view/manage/%/delete'] = array(
'route_name' => 'entity_view_mode.delete',
);
// Form modes.
$items['admin/structure/display-modes/form'] = array(
'title' => 'Form modes',
'description' => 'Manage custom form modes.',
'route_name' => 'entity_form_mode.list',
);
$items['admin/structure/display-modes/form/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/display-modes/form/add'] = array(
'title' => 'Add form mode',
'route_name' => 'entity_form_mode.add',
'type' => MENU_SIBLING_LOCAL_TASK,
);
$items['admin/structure/display-modes/form/add/%'] = array(
'route_name' => 'entity_form_mode.add_type',
);
$items['admin/structure/display-modes/form/manage/%'] = array(
'title' => 'Edit form mode',
'route_name' => 'entity_form_mode.edit',
);
$items['admin/structure/display-modes/form/manage/%/delete'] = array(
'route_name' => 'entity_form_mode.delete',
);
return $items;
}
/**
* Implements hook_local_actions().
*/
function entity_local_actions() {
return array(
array(
'route_name' => 'entity_view_mode.add',
'title' => t('Add view mode'),
'appears_on' => array(
'entity_view_mode.list',
),
),
);
}
/**
* Implements hook_entity_bundle_rename().
*/
......
entity_view_mode.list:
pattern: '/admin/structure/display-modes/view'
defaults:
_entity_list: 'view_mode'
requirements:
_permission: 'administer display modes'
entity_view_mode.add:
pattern: '/admin/structure/display-modes/view/add'
defaults:
_content: '\Drupal\entity\Controller\EntityDisplayModeController::viewModeTypeSelection'
requirements:
_permission: 'administer display modes'
entity_view_mode.add_type:
pattern: '/admin/structure/display-modes/view/add/{entity_type}'
defaults:
_entity_form: 'view_mode.add'
requirements:
_permission: 'administer display modes'
entity_view_mode.edit:
pattern: '/admin/structure/display-modes/view/manage/{view_mode}'
defaults:
_entity_form: 'view_mode.edit'
requirements:
_entity_access: 'view_mode.update'
entity_view_mode.delete:
pattern: '/admin/structure/display-modes/view/manage/{view_mode}/delete'
defaults:
_entity_form: 'view_mode.delete'
requirements:
_entity_access: 'view_mode.delete'
entity_form_mode.list:
pattern: '/admin/structure/display-modes/form'
defaults:
_entity_list: 'form_mode'
requirements:
_permission: 'administer display modes'
entity_form_mode.add:
pattern: '/admin/structure/display-modes/form/add'
defaults:
_content: '\Drupal\entity\Controller\EntityDisplayModeController::formModeTypeSelection'
requirements:
_permission: 'administer display modes'
entity_form_mode.add_type:
pattern: '/admin/structure/display-modes/form/add/{entity_type}'
defaults:
_entity_form: 'form_mode.add'
requirements:
_permission: 'administer display modes'
entity_form_mode.edit:
pattern: '/admin/structure/display-modes/form/manage/{form_mode}'
defaults:
_entity_form: 'form_mode.edit'
requirements:
_entity_access: 'form_mode.update'
entity_form_mode.delete:
pattern: '/admin/structure/display-modes/form/manage/{form_mode}/delete'
defaults:
_entity_form: 'form_mode.delete'
requirements:
_entity_access: 'form_mode.delete'
<?php
/**
* @file
* Contains \Drupal\entity\Controller\EntityDisplayModeController.
*/
namespace Drupal\entity\Controller;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Controller\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides methods for entity display mode routes.
*/
class EntityDisplayModeController implements ControllerInterface {
/**
* The entity manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $entityManager;
/**
* Constructs a new EntityDisplayModeFormBase.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(PluginManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')
);
}
/**
* Provides a list of eligible entity types for adding view modes.
*
* @return array
* A list of entity types to add a view mode for.
*/
public function viewModeTypeSelection() {
drupal_set_title(t('Choose view mode entity type'));
$entity_types = array();
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
if ($entity_info['fieldable'] && isset($entity_info['controllers']['render'])) {
$entity_types[$entity_type] = array(
'title' => $entity_info['label'],
'href' => 'admin/structure/display-modes/view/add/' . $entity_type,
'localized_options' => array(),
);
}
}
return array(
'#theme' => 'admin_block_content',
'#content' => $entity_types,
);
}
/**
* Provides a list of eligible entity types for adding form modes.
*
* @return array
* A list of entity types to add a form mode for.
*/
public function formModeTypeSelection() {
drupal_set_title(t('Choose form mode entity type'));
$entity_types = array();
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
if ($entity_info['fieldable'] && isset($entity_info['controllers']['form'])) {
$entity_types[$entity_type] = array(
'title' => $entity_info['label'],
'href' => 'admin/structure/display-modes/form/add/' . $entity_type,
'localized_options' => array(),
);
}
}
return array(
'#theme' => 'admin_block_content',
'#content' => $entity_types,
);
}
}
<?php
/**
* @file
* Contains \Drupal\entity\EntityDisplayModeAccessController.
*/
namespace Drupal\entity;
use Drupal\Core\Entity\EntityAccessController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Provides the access controller for entity display modes.
*/
class EntityDisplayModeAccessController extends EntityAccessController {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) {
if ($operation === 'view') {
return TRUE;
}
elseif (in_array($operation, array('create', 'update', 'delete'))) {
return user_access('administer display modes', $account);
}
}
}
......@@ -12,7 +12,7 @@
/**
* Base class for config entity types that hold settings for form and view modes.
*/
abstract class EntityDisplayModeBase extends ConfigEntityBase {
abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityDisplayModeInterface {
/**
* The ID of the form or view mode.
......@@ -38,8 +38,8 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase {
/**
* The entity type this form or view mode is used for.
*
* This is not to be confused with EntityViewMode::entityType which is
* inherited from Entity::entityType.
* This is not to be confused with EntityDisplayModeBase::$entityType which is
* inherited from Entity::$entityType.
*
* @var string
*/
......@@ -55,6 +55,24 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase {
*
* @var bool
*/
public $status = FALSE;
public $status = TRUE;
/**
* {@inheritdoc}
*/
public static function sort($a, $b) {
// Sort by the type of entity the view mode is used for.
$a_type = $a->getTargetType();
$b_type = $b->getTargetType();
$type_order = strnatcasecmp($a_type, $b_type);
return $type_order != 0 ? $type_order : parent::sort($a, $b);
}
/**
* {@inheritdoc}
*/
public function getTargetType() {
return $this->targetEntityType;
}
}
<?php
/**
* @file
* Contains \Drupal\entity\EntityDisplayModeInterface.
*/
namespace Drupal\entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for entity types that hold form and view mode settings.
*/
interface EntityDisplayModeInterface extends ConfigEntityInterface {
/**
* Returns the entity type this display mode is used for.
*
* @return string
* The entity type name.
*/
public function getTargetType();
}
<?php
/**
* @file
* Contains \Drupal\entity\EntityDisplayModeListController.
*/
namespace Drupal\entity;
use Drupal\Component\Utility\String;
use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the listing for entity display modes.
*/
class EntityDisplayModeListController extends ConfigEntityListController {
/**
* The entity info for all entity types.
*
* @var array
*/
protected $entityInfoComplete;
/**
* Constructs a new EntityListController object.
*
* @param string $entity_type
* The type of entity to be listed.
* @param array $entity_info
* An array of entity info for the entity type.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage
* The entity storage controller class.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke hooks on.
* @param array $entity_info_complete
* The entity info for all entity types.
*/
public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, array $entity_info_complete) {
parent::__construct($entity_type, $entity_info, $storage, $module_handler);
$this->entityInfoComplete = $entity_info_complete;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
$entity_manager = $container->get('plugin.manager.entity');
return new static(
$entity_type,
$entity_info,
$entity_manager->getStorageController($entity_type),
$container->get('module_handler'),
$entity_manager->getDefinitions()
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = parent::buildHeader();
unset($header['id']);
return $header;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row = parent::buildRow($entity);
unset($row['id']);
return $row;
}
/**
* {@inheritdoc}
*/
public function load() {
$entities = array();
foreach (parent::load() as $entity) {
$entities[$entity->getTargetType()][] = $entity;
}
return $entities;
}
/**
* {@inheritdoc}
*/
public function render() {
$build = array();
foreach ($this->load() as $entity_type => $entities) {
if (!isset($this->entityInfoComplete[$entity_type])) {
continue;
}
// Filter entities
if ($this->entityInfoComplete[$entity_type]['fieldable'] && !$this->isValidEntity($entity_type)) {
continue;
}
$table = array(
'#prefix' => '<h2>' . $this->entityInfoComplete[$entity_type]['label'] . '</h2>',
'#type' => 'table',
'#header' => $this->buildHeader(),
'#rows' => array(),
);
foreach ($entities as $entity) {
if ($row = $this->buildRow($entity)) {
$table['#rows'][$entity->id()] = $row;
}
}
// Move content at the top.
if ($entity_type == 'node') {
$table['#weight'] = -10;
}
$short_type = str_replace('_mode', '', $this->entityType);
$table['#rows']['_add_new'][] = array(
'data' => array(
'#type' => 'link',
'#href' => "admin/structure/display-modes/$short_type/add/$entity_type",
'#title' => t('Add new %label @entity-type', array('%label' => $this->entityInfoComplete[$entity_type]['label'], '@entity-type' => strtolower($this->entityInfo['label']))),
'#options' => array(
'html' => TRUE,
),
),
'colspan' => count($table['#header']),
);
$build[$entity_type] = $table;
}
return $build;
}
/**
* Filters entities based on their controllers.
*
* @param $entity_type
* The entity type of the entity that needs to be validated.
*
* @return bool
* TRUE if the entity has the correct controller, FALSE if the entity
* doesn't has the correct controller.
*/
protected function isValidEntity($entity_type) {
return isset($this->entityInfoComplete[$entity_type]['controllers']['render']);
}
}
......@@ -7,11 +7,9 @@
namespace Drupal\entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining an entity form mode entity type.
*/
interface EntityFormModeInterface extends ConfigEntityInterface {
interface EntityFormModeInterface extends EntityDisplayModeInterface {
}
<?php
/**
* @file
* Contains \Drupal\entity\EntityFormModeListController.
*/
namespace Drupal\entity;
/**
* Provides the listing for entity display modes.
*/
class EntityFormModeListController extends EntityDisplayModeListController {
/**
* Filters entities based on their controllers.
*
* @param $entity_type
* The entity type of the entity that needs to be validated.
*
* @return bool
* TRUE if the entity has the correct controller, FALSE if the entity
* doesn't has the correct controller.
*/
protected function isValidEntity($entity_type) {
return isset($this->entityInfoComplete[$entity_type]['controllers']['form']);
}
}
......@@ -7,11 +7,9 @@
namespace Drupal\entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining an entity view mode entity type.
*/
interface EntityViewModeInterface extends ConfigEntityInterface {
interface EntityViewModeInterface extends EntityDisplayModeInterface {
}
<?php
/**
* @file
* Contains \Drupal\entity\Form\EntityDisplayModeAddForm.
*/
namespace Drupal\entity\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides the add form for entity display modes.
*/
class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
/**
* @var string
*/
protected $entityType;
/**
* The entity manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $entityManager;
/**
* Constructs a new EntityDisplayModeAddForm.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query factory.
* @param array $entity_info
* The entity type definition.
* @param \Drupal\Component\Plugin\PluginManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query_factory, array $entity_info, PluginManagerInterface $entity_manager) {
parent::__construct($module_handler, $query_factory, $entity_info);
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static(
$container->get('module_handler'),
$container->get('entity.query'),
$entity_info,
$container->get('plugin.manager.entity')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $entity_type = NULL) {
$this->entityType = $entity_type;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
form_set_value($form['id'], $this->entityType . '.' . $form_state['values']['id'], $form_state);
}
/**
* {@inheritdoc}
*/
protected function prepareEntity() {
$definition = $this->entityManager->getDefinition($this->entityType);
if (!$definition['fieldable'] || !isset($definition['controllers']['render'])) {
throw new NotFoundHttpException();
}
drupal_set_title(t('Add new %label @entity-type', array('%label' => $definition['label'], '@entity-type' => strtolower($this->entityInfo['label']))), PASS_THROUGH);
$this->entity->targetEntityType = $this->entityType;
}
}
<?php
/**
* @file
* Contains \Drupal\entity\Form\EntityDisplayModeDeleteForm.
*/
namespace Drupal\entity\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
/**
* Provides the delete form for entity display modes.
*/
class EntityDisplayModeDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
$short_type = str_replace('_mode', '', $this->entity->entityType());
return "admin/structure/display-modes/$short_type";
}
/**
* {@inheritdoc}
*/
public function getQuestion() {