Commit fb90a9df authored by jsacksick's avatar jsacksick
Browse files

Issue #3245682 by jsacksick: Field UI for pricelist and price list items.

parent 5b101084
......@@ -3,3 +3,21 @@ entity.commerce_pricelist.collection:
route_name: 'entity.commerce_pricelist.collection'
description: 'Manage your price lists'
parent: 'commerce.admin_commerce'
commerce_pricelist.configuration:
title: 'Price lists'
route_name: 'commerce_pricelist.configuration'
parent: 'commerce.configuration'
weight: -5
entity.commerce_pricelist.bundle_list:
title: 'Price list types'
route_name: 'entity.commerce_pricelist.bundle_list'
parent: 'commerce_pricelist.configuration'
description: 'Manage fields, form and display settings for your price lists.'
entity.commerce_pricelist_item.bundle_list:
title: 'Price list item types'
route_name: 'entity.commerce_pricelist_item.bundle_list'
parent: 'commerce_pricelist.configuration'
description: 'Manage fields, form and display settings for your price list items.'
......@@ -15,3 +15,55 @@ entity.commerce_product_variation.add_price_form:
commerce_product: '\d+'
commerce_product_variation: '\d+'
_entity_create_access: 'commerce_pricelist_item'
entity.commerce_pricelist.bundle_list:
path: '/admin/commerce/config/price-lists/types'
defaults:
_controller: 'Drupal\commerce_pricelist\Controller\PriceListBundleController::adminPage'
_title: 'Price list types'
requirements:
_permission: 'administer commerce_pricelist'
options:
_admin_route: 'TRUE'
entity.commerce_pricelist_item.bundle_list:
path: '/admin/commerce/config/price-lists/item-types'
defaults:
_controller: 'Drupal\commerce_pricelist\Controller\PriceListBundleController::adminPage'
_title: 'Price list item types'
requirements:
_permission: 'administer commerce_pricelist'
options:
_admin_route: 'TRUE'
entity.commerce_pricelist.bundle_page:
path: '/admin/commerce/config/price-lists/types/{bundle}'
defaults:
_controller: 'Drupal\commerce_pricelist\Controller\PriceListBundleController::bundlePage'
_title: 'Settings'
requirements:
_permission: 'administer commerce_pricelist'
options:
_admin_route: 'TRUE'
parameters:
bundle: 'string'
entity.commerce_pricelist_item.bundle_page:
path: '/admin/commerce/config/price-lists/item-types/{bundle}'
defaults:
_controller: 'Drupal\commerce_pricelist\Controller\PriceListBundleController::bundlePage'
_title: 'Settings'
requirements:
_permission: 'administer commerce_pricelist'
options:
_admin_route: 'TRUE'
parameters:
bundle: 'string'
commerce_pricelist.configuration:
path: '/admin/commerce/config/price-lists'
defaults:
_controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
_title: 'Price lists'
requirements:
_permission: 'access commerce administration pages'
<?php
namespace Drupal\commerce_pricelist\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the price list/item controller for bundle fields.
*/
class PriceListBundleController extends ControllerBase {
use StringTranslationTrait;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The Entity Type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type ID.
*
* @var string
*/
protected $entityTypeId;
/**
* Constructs a new PriceListBundleController object.
*
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityTypeManager = $entity_type_manager;
$route_name_parts = explode('.', $route_match->getRouteName());
// Extract the entity type ID from the route name.
$this->entityTypeId = $route_name_parts[1];
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.bundle.info'),
$container->get('entity_type.manager'),
$container->get('current_route_match')
);
}
/**
* Callback for the field UI base route.
*/
public function bundlePage($bundle = NULL) {
$entity_bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId);
return [
'#markup' => $this->t('The @bundle-label bundle has no settings.', [
'@bundle-label' => $entity_bundle_info[$bundle]['label'],
]),
];
}
/**
* Handles base route for the field UI.
*
* Field UI needs some base route to attach its routes to.
*
* @return array
* The page content.
*/
public function adminPage() {
$entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
$entity_bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId);
$build = [];
$build['table'] = [
'#type' => 'table',
'#header' => [
$this->t('Name'),
$this->t('Description'),
$this->t('Operations'),
],
'#rows' => [],
'#empty' => $this->t('There are no @label yet.', [
'@label' => $entity_type->getPluralLabel(),
]),
];
foreach ($entity_bundle_info as $bundle_name => $bundle_info) {
$build['table']['#rows'][$bundle_name] = [
'name' => ['data' => $bundle_info['label']],
'description' => ['data' => $bundle_info['description'] ?? ''],
'operations' => ['data' => $this->buildOperations($bundle_name)],
];
}
return $build;
}
/**
* Builds a renderable list of operation links for the bundle.
*
* @return array
* A renderable array of operation links.
*
* @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
*/
protected function buildOperations($bundle_name) {
$operations = [
'manage-fields' => [
'title' => t('Manage fields'),
'weight' => 15,
'url' => Url::fromRoute("entity.{$this->entityTypeId}.field_ui_fields", [
'bundle' => $bundle_name,
]),
],
'manage-form-display' => [
'title' => t('Manage form display'),
'weight' => 20,
'url' => Url::fromRoute("entity.entity_form_display.{$this->entityTypeId}.default", [
'bundle' => $bundle_name,
]),
],
'manage-display' => [
'title' => t('Manage display'),
'weight' => 25,
'url' => Url::fromRoute("entity.entity_view_display.{$this->entityTypeId}.default", [
'bundle' => $bundle_name,
]),
],
];
return [
'#type' => 'operations',
'#links' => $operations,
];
}
}
......@@ -67,6 +67,7 @@ use Drupal\user\UserInterface;
* "delete-multiple-form" = "/admin/commerce/price-lists/delete",
* "collection" = "/admin/commerce/price-lists",
* },
* field_ui_base_route = "entity.commerce_pricelist.bundle_list"
* )
*/
class PriceList extends CommerceContentEntityBase implements PriceListInterface {
......@@ -328,6 +329,7 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface
->setSetting('target_type', 'commerce_store')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_entity_select',
'weight' => 2,
......@@ -374,6 +376,7 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface
->setRequired(FALSE)
->setSetting('datetime_type', 'datetime')
->setSetting('datetime_optional_label', t('Provide an end date'))
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_store_datetime',
'weight' => 6,
......@@ -393,6 +396,7 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface
'on_label' => t('Enabled'),
'off_label' => t('Disabled'),
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
]);
......
......@@ -66,6 +66,7 @@ use Drupal\user\EntityOwnerTrait;
* "import-form" = "/price-list/{commerce_pricelist}/prices/import",
* "collection" = "/price-list/{commerce_pricelist}/prices",
* },
* field_ui_base_route = "entity.commerce_pricelist_item.bundle_page"
* )
*/
class PriceListItem extends CommerceContentEntityBase implements PriceListItemInterface {
......@@ -249,6 +250,7 @@ class PriceListItem extends CommerceContentEntityBase implements PriceListItemIn
->setLabel(t('Purchasable entity'))
->setDescription(t('The purchasable entity.'))
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
......@@ -271,6 +273,7 @@ class PriceListItem extends CommerceContentEntityBase implements PriceListItemIn
->setSetting('unsigned', TRUE)
->setSetting('min', 0)
->setDefaultValue(1)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_quantity',
]);
......@@ -282,6 +285,7 @@ class PriceListItem extends CommerceContentEntityBase implements PriceListItemIn
'label' => 'above',
'type' => 'commerce_price_default',
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_list_price',
]);
......@@ -294,6 +298,7 @@ class PriceListItem extends CommerceContentEntityBase implements PriceListItemIn
'label' => 'above',
'type' => 'commerce_price_default',
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_price_default',
]);
......@@ -307,6 +312,7 @@ class PriceListItem extends CommerceContentEntityBase implements PriceListItemIn
'on_label' => t('Enabled'),
'off_label' => t('Disabled'),
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
]);
......
......@@ -15,7 +15,7 @@ class PriceListForm extends ContentEntityForm {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\commerce_pricelist\Entity\PriceList */
/** @var \Drupal\commerce_pricelist\Entity\PriceList $entity */
$store_query = $this->entityTypeManager->getStorage('commerce_store')->getQuery();
if ($store_query->count()->execute() == 0) {
$link = Link::createFromRoute('Add a new store.', 'entity.commerce_store.add_page');
......
......@@ -93,7 +93,7 @@ class PriceListItemListBuilder extends EntityListBuilder {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var \Drupal\commerce_pricelist\Entity\PriceListItemInterface $entity */
/** @var \Drupal\commerce_pricelist\Entity\PriceListItemInterface $entity */
$purchasable_entity = $entity->getPurchasableEntity();
$row['purchasable_entity'] = $purchasable_entity ? $purchasable_entity->getOrderItemTitle() : '';
$row['quantity'] = Calculator::trim($entity->getQuantity());
......
......@@ -103,7 +103,7 @@ class PriceListListBuilder extends EntityListBuilder implements FormInterface {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var \Drupal\commerce_pricelist\Entity\PriceListInterface $entity */
/** @var \Drupal\commerce_pricelist\Entity\PriceListInterface $entity */
$row['#attributes']['class'][] = 'draggable';
$row['#weight'] = $entity->getWeight();
$row['name'] = $entity->label();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment