diff --git a/config/sync/core.entity_form_display.commerce_order.materio_order_type.default.yml b/config/sync/core.entity_form_display.commerce_order.materio_order_type.default.yml index 7a426e48..ba2e85bf 100644 --- a/config/sync/core.entity_form_display.commerce_order.materio_order_type.default.yml +++ b/config/sync/core.entity_form_display.commerce_order.materio_order_type.default.yml @@ -40,13 +40,13 @@ content: override_labels: true label_singular: 'order item' label_plural: 'order items' + allow_new: true match_operator: CONTAINS + revision: false collapsible: false collapsed: false - allow_new: false allow_existing: false allow_duplicate: false - revision: false third_party_settings: { } region: content hidden: diff --git a/web/modules/custom/materio_commerce/materio_commerce.module b/web/modules/custom/materio_commerce/materio_commerce.module index a071b8ef..11295094 100644 --- a/web/modules/custom/materio_commerce/materio_commerce.module +++ b/web/modules/custom/materio_commerce/materio_commerce.module @@ -8,4 +8,16 @@ function materio_commerce_form_alter(&$form, &$form_state, $form_id) { $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'; + } } \ No newline at end of file diff --git a/web/modules/custom/materio_commerce/materio_commerce.services.yml b/web/modules/custom/materio_commerce/materio_commerce.services.yml index 169fccfa..709cb1ab 100644 --- a/web/modules/custom/materio_commerce/materio_commerce.services.yml +++ b/web/modules/custom/materio_commerce/materio_commerce.services.yml @@ -2,4 +2,13 @@ services: materio_commerce.order_events_subscriber: class: Drupal\materio_commerce\EventSubscriber\MaterioCommerceOrderEventsSubscriber tags: - - { name: 'event_subscriber' } \ No newline at end of file + - { 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'] \ No newline at end of file diff --git a/web/modules/custom/materio_commerce/src/Controller/EntityAutocompleteController.php b/web/modules/custom/materio_commerce/src/Controller/EntityAutocompleteController.php new file mode 100644 index 00000000..84adb02d --- /dev/null +++ b/web/modules/custom/materio_commerce/src/Controller/EntityAutocompleteController.php @@ -0,0 +1,34 @@ +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') + ); + } + +} \ No newline at end of file diff --git a/web/modules/custom/materio_commerce/src/EntityAutocompleteMatcher.php b/web/modules/custom/materio_commerce/src/EntityAutocompleteMatcher.php new file mode 100644 index 00000000..addd2021 --- /dev/null +++ b/web/modules/custom/materio_commerce/src/EntityAutocompleteMatcher.php @@ -0,0 +1,62 @@ + $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; + } + +} \ No newline at end of file diff --git a/web/modules/custom/materio_commerce/src/Plugin/EntityReferenceSelection/UserByEmailSelection.php b/web/modules/custom/materio_commerce/src/Plugin/EntityReferenceSelection/UserByEmailSelection.php new file mode 100644 index 00000000..23e041b3 --- /dev/null +++ b/web/modules/custom/materio_commerce/src/Plugin/EntityReferenceSelection/UserByEmailSelection.php @@ -0,0 +1,55 @@ +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"; + + } + } + } + +} diff --git a/web/modules/custom/materio_commerce/src/Routing/AutocompleteRouteSubscriber.php b/web/modules/custom/materio_commerce/src/Routing/AutocompleteRouteSubscriber.php new file mode 100644 index 00000000..994171be --- /dev/null +++ b/web/modules/custom/materio_commerce/src/Routing/AutocompleteRouteSubscriber.php @@ -0,0 +1,16 @@ +get('system.entity_autocomplete')) { + $route->setDefault('_controller', '\Drupal\materio_commerce\Controller\EntityAutocompleteController::handleAutocomplete'); + } + } + +} \ No newline at end of file