improved first step of create order form

This commit is contained in:
Bachir Soussi Chiadmi 2021-08-23 00:46:52 +02:00
parent a39da8fc36
commit 05b2621bc7
7 changed files with 191 additions and 3 deletions

View File

@ -40,13 +40,13 @@ content:
override_labels: true override_labels: true
label_singular: 'order item' label_singular: 'order item'
label_plural: 'order items' label_plural: 'order items'
allow_new: true
match_operator: CONTAINS match_operator: CONTAINS
revision: false
collapsible: false collapsible: false
collapsed: false collapsed: false
allow_new: false
allow_existing: false allow_existing: false
allow_duplicate: false allow_duplicate: false
revision: false
third_party_settings: { } third_party_settings: { }
region: content region: content
hidden: hidden:

View File

@ -8,4 +8,16 @@ function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['next']['#value'] = t('Place your order'); $form['actions']['next']['#value'] = t('Place your order');
} }
} }
}
function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_state, $form_id) {
$currentUser = \Drupal::currentUser();
$roles = $currentUser->getRoles();
if (in_array("admin", $roles)){
$t="t";
$form['type']['#default_value'] = 'materio_order_type';
$form['type']['#disabled'] = true;
$form['customer']['uid']['#selection_handler'] = 'default:user_by_email';
}
} }

View File

@ -2,4 +2,13 @@ services:
materio_commerce.order_events_subscriber: materio_commerce.order_events_subscriber:
class: Drupal\materio_commerce\EventSubscriber\MaterioCommerceOrderEventsSubscriber class: Drupal\materio_commerce\EventSubscriber\MaterioCommerceOrderEventsSubscriber
tags: tags:
- { name: 'event_subscriber' } - { name: 'event_subscriber' }
materio_commerce.route_subscriber:
class: Drupal\materio_commerce\Routing\AutocompleteRouteSubscriber
tags:
- { name: event_subscriber }
materio_commerce.autocomplete_matcher:
class: Drupal\materio_commerce\EntityAutocompleteMatcher
arguments: ['@plugin.manager.entity_reference_selection']

View File

@ -0,0 +1,34 @@
<?php
namespace Drupal\materio_commerce\Controller;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\materio_commerce\EntityAutocompleteMatcher;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityAutocompleteController extends \Drupal\system\Controller\EntityAutocompleteController {
/**
* The autocomplete matcher for entity references.
*/
protected $matcher;
/**
* {@inheritdoc}
*/
public function __construct(EntityAutocompleteMatcher $matcher, KeyValueStoreInterface $key_value) {
$this->matcher = $matcher;
$this->keyValue = $key_value;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('materio_commerce.autocomplete_matcher'),
$container->get('keyvalue')->get('entity_autocomplete')
);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Drupal\materio_commerce;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
class EntityAutocompleteMatcher extends \Drupal\Core\Entity\EntityAutocompleteMatcher {
/**
* Gets matched labels based on a given search string.
*/
public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') {
$matches = [];
$options = [
'target_type' => $target_type,
'handler' => $selection_handler,
'handler_settings' => $selection_settings,
];
$handler = $this->selectionManager->getInstance($options);
if (isset($string)) {
// Get an array of matching entities.
$match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS';
$entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);
// Loop through the entities and convert them into autocomplete output.
foreach ($entity_labels as $values) {
foreach ($values as $entity_id => $label) {
$entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($entity_id);
$entity = \Drupal::entityManager()->getTranslationFromContext($entity);
$key = $label . ' (' . $entity_id . ')';
// Strip things like starting/trailing white spaces, line breaks and tags.
$key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
$key = Tags::encode($key);
switch ($entity->getEntityType()->id()) {
case 'node':
$type = !empty($entity->type->entity) ? $entity->type->entity->label() : $entity->bundle();
$status = ($entity->isPublished()) ? ", Published" : ", Unpublished";
break;
case 'user':
$email = $entity->getEmail();
$label = $label . ' (' . $entity_id . ') [' . $email . ']';
break;
}
$matches[] = ['value' => $key, 'label' => $label];
}
}
}
return $matches;
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Drupal\materio_commerce\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\RoleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\user\Plugin\EntityReferenceSelection\UserSelection;
/**
* Provides specific access control for the user entity type.
*
* @EntityReferenceSelection(
* id = "default:user_by_email",
* label = @Translation("User selection By Email"),
* entity_types = {"user"},
* group = "default",
* weight = 3
* )
*/
class UserByEmailSelection extends UserSelection {
/**
* {@inheritdoc}
*/
public function entityQueryAlter(SelectInterface $query) {
parent::entityQueryAlter($query);
$conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users_field_data.name') {
unset($conditions[$key]);
$or = new Condition('OR');
$or->condition('users_field_data.name', $condition['value'], $condition['operator']);
$or->condition('mail', $condition['value'], $condition['operator']);
$query->condition($or);
$t="t";
}
}
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Drupal\materio_commerce\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
class AutocompleteRouteSubscriber extends RouteSubscriberBase {
public function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.entity_autocomplete')) {
$route->setDefault('_controller', '\Drupal\materio_commerce\Controller\EntityAutocompleteController::handleAutocomplete');
}
}
}