Skip to content
Snippets Groups Projects
Commit 387b978a authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Jonathan Sacksick
Browse files

Issue #2689919 by joshmiller, jeffam, Morbus Iff, jsacksick, themic8, joachim,...

Issue #2689919 by joshmiller, jeffam, Morbus Iff, jsacksick, themic8, joachim, a-fro, Marcin Maruszewski, tim_dj, facine: Allow the view in the OrderItemTable formatter to be configured.
parent 44d6091e
No related branches found
No related tags found
2 merge requests!235Issue #3115150 by mbovan: Submitting add to cart form with empty quantity...,!205Issue #3349465 by tBKoT, jsacksick, bojanz: Update several VAT rates and setup...
......@@ -243,3 +243,19 @@ function commerce_order_post_update_15(&$sandbox) {
$config->set('log_version_mismatch', TRUE);
$config->save();
}
/**
* Add a tag to the commerce_order_item_table view.
*/
function commerce_order_post_update_16() {
$config_factory = \Drupal::configFactory();
$view = $config_factory->getEditable('views.view.commerce_order_item_table');
if (!str_contains($view->get('tag'), 'commerce_order_item_table')) {
$tags = $view->get('tag');
$view->set('tag', empty($tags)
? 'commerce_order_item_table'
: 'commerce_order_item_table, ' . $tags
);
$view->save(TRUE);
}
}
......@@ -8,7 +8,7 @@ id: commerce_order_item_table
label: 'Order items'
module: views
description: 'Display a set of order items in a table.'
tag: ''
tag: 'commerce_order_item_table'
base_table: commerce_order_item
base_field: order_item_id
display:
......
......@@ -156,3 +156,10 @@ profile.type.*.third_party.commerce_order:
customer_profile_type:
type: boolean
label: 'Profiles of this type represent Commerce customer profiles'
field.formatter.settings.commerce_order_item_table:
type: mapping
mapping:
view:
type: string
label: 'Order item table view'
......@@ -2,9 +2,15 @@
namespace Drupal\commerce_order\Plugin\Field\FieldFormatter;
use Drupal\commerce\AjaxFormTrait;
use Drupal\commerce\EntityHelper;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\ViewEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'commerce_order_item_table' formatter.
......@@ -17,7 +23,72 @@ use Drupal\Core\Field\FormatterBase;
* },
* )
*/
class OrderItemTable extends FormatterBase {
class OrderItemTable extends FormatterBase implements ContainerFactoryPluginInterface {
use AjaxFormTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->entityTypeManager = $container->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'view' => 'commerce_order_item_table',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$view_storage = $this->entityTypeManager->getStorage('view');
$default_view = $this->getSetting('view');
$applicable_views = array_filter($view_storage->loadMultiple(), function (ViewEntityInterface $view) {
return str_contains($view->get('tag'), 'commerce_order_item_table') ||
$view->id() === $this->getSetting('view');
});
$elements['view'] = [
'#type' => 'select',
'#title' => $this->t('Order item table view'),
'#description' => $this->t("Only views tagged with 'commerce_order_item_table' are displayed."),
'#options' => EntityHelper::extractLabels($applicable_views),
'#required' => TRUE,
'#default_value' => $default_view,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$view = $this->entityTypeManager->getStorage('view')->load($this->getSetting('view'));
$summary[] = $this->t('View: @view.', [
'@view' => $view->label(),
]);
return $summary;
}
/**
* {@inheritdoc}
......@@ -29,8 +100,7 @@ class OrderItemTable extends FormatterBase {
$order_item_ids = array_column($order->get('order_items')->getValue(), 'target_id');
$elements[0] = [
'#type' => 'view',
// @todo Allow the view to be configurable.
'#name' => 'commerce_order_item_table',
'#name' => $this->getSetting('view'),
'#arguments' => $order_item_ids ? [implode('+', $order_item_ids)] : NULL,
'#embed' => TRUE,
];
......@@ -42,9 +112,7 @@ class OrderItemTable extends FormatterBase {
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition->getTargetEntityTypeId();
$field_name = $field_definition->getName();
return $entity_type == 'commerce_order' && $field_name == 'order_items';
return $field_definition->getTargetEntityTypeId() === 'commerce_order' && $field_definition->getName() === 'order_items';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment