commerce_inventory-8.x-1.0-alpha6/commerce_inventory.module
commerce_inventory.module
<?php
/**
* @file
* Contains commerce_inventory.module.
*/
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function commerce_inventory_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the commerce_inventory module.
case 'help.page.commerce_inventory':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Manage inventory of purchasable items.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function commerce_inventory_theme() {
$theme = [];
$theme['commerce_inventory_item'] = [
'render element' => 'elements',
'file' => 'commerce_inventory_item.page.inc',
'template' => 'commerce_inventory_item',
];
$theme['commerce_inventory_location'] = [
'render element' => 'elements',
'file' => 'commerce_inventory_location.page.inc',
'template' => 'commerce_inventory_location',
];
return $theme;
}
/**
* Implements hook_entity_form_display_alter().
*/
function commerce_inventory_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
// Get relevant plugin manager service IDs by entity-type.
switch ($context['entity_type']) {
case 'commerce_inventory_item':
$configuration_key = 'item_remote_id_required';
break;
case 'commerce_inventory_location':
$configuration_key = 'location_remote_id_required';
break;
default:
$configuration_key = NULL;
}
// Hide Remote ID for providers that don't require it.
if (!is_null($configuration_key)) {
$definition = \Drupal::service('plugin.manager.commerce_inventory_provider')->getDefinition($context['bundle']);
if (is_array($definition) && isset($definition[$configuration_key]) && $definition[$configuration_key] == FALSE) {
$form_display->setComponent('remote_id', [
'type' => 'hidden',
]);
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for views_form_commerce_inventory_commerce_inventory_item_admin_location.
*/
function commerce_inventory_form_views_form_commerce_inventory_commerce_inventory_item_admin_location_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#submit'][] = 'commerce_inventory_form_views_form_commerce_inventory_commerce_inventory_item_admin_location_submit';
}
/**
* Submit callback set in commerce_inventory_form_views_form_commerce_inventory_commerce_inventory_item_admin_location_alter().
*/
function commerce_inventory_form_views_form_commerce_inventory_commerce_inventory_item_admin_location_submit(&$form, FormStateInterface $form_state) {
// Fix to pass current route parameters into view bulk operations.
// Follow https://www.drupal.org/node/2901412.
if ($form_state->getRedirect()) {
foreach (\Drupal::routeMatch()->getRawParameters()->all() as $key => $parameter) {
$form_state->getRedirect()->setRouteParameter($key, $parameter);
}
}
}
