From 6b44502894c8f31d094f065c385e0934e2fb86e8 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Fri, 2 Sep 2016 16:47:27 -0400 Subject: [PATCH 01/12] WIP Issue #2794531: Wishlist Templates --- templates/wishlist_template.info.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 templates/wishlist_template.info.yml diff --git a/templates/wishlist_template.info.yml b/templates/wishlist_template.info.yml new file mode 100644 index 0000000..08d262f --- /dev/null +++ b/templates/wishlist_template.info.yml @@ -0,0 +1,7 @@ +name: Commerce Wishlist Template +type: module +description: Provides the ability to create product category lists that change the way a wishlist is created and presented. +package: Commerce (contrib) +core: 8.x +dependencies: + - commerce_wishlist From 51cab16fdc0317ddd61464c67b085df51a892741 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Mon, 5 Sep 2016 16:37:22 -0400 Subject: [PATCH 02/12] WIP Issue #2794531: Wishlist Templates - Scaffolding in place --- 2791939-03.patch | 260 ------------------ templates/wishlist_template.info.yml | 7 - ...play.wishlist_template.default.default.yml | 16 ++ ...emplate.wishlist_template_type.default.yml | 6 + .../schema/wishlist_template.schema.yml | 22 ++ .../EntityWishlistTemplateInterface.php | 46 ++++ .../src/Entity/WishlistTemplate.php | 222 +++++++++++++++ .../src/Entity/WishlistTemplateInterface.php | 67 +++++ .../src/Entity/WishlistTemplateType.php | 90 ++++++ .../Entity/WishlistTemplateTypeInterface.php | 12 + .../src/Event/WishlistTemplateEvent.php | 42 +++ .../src/Event/WishlistTemplateEvents.php | 90 ++++++ .../src/Form/WishlistTemplateForm.php | 35 +++ .../Form/WishlistTemplateTypeDeleteForm.php | 59 ++++ .../src/Form/WishlistTemplateTypeForm.php | 73 +++++ .../Plugin/views/field/WishlistTemplate.php | 56 ++++ .../src/WishlistTemplateListBuilder.php | 39 +++ .../src/WishlistTemplateStorage.php | 66 +++++ .../src/WishlistTemplateTypeListBuilder.php | 31 +++ .../templates/wishlist-template.html.twig | 24 ++ wishlist_template/wishlist_template.info.yml | 15 + .../wishlist_template.links.action.yml | 11 + .../wishlist_template.links.menu.yml | 11 + .../wishlist_template.links.task.yml | 24 ++ wishlist_template/wishlist_template.module | 70 +++++ .../wishlist_template.permissions.yml | 9 + .../wishlist_template.routing.yml | 15 + 27 files changed, 1151 insertions(+), 267 deletions(-) delete mode 100644 2791939-03.patch delete mode 100644 templates/wishlist_template.info.yml create mode 100644 wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml create mode 100644 wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml create mode 100644 wishlist_template/config/schema/wishlist_template.schema.yml create mode 100644 wishlist_template/src/Entity/EntityWishlistTemplateInterface.php create mode 100644 wishlist_template/src/Entity/WishlistTemplate.php create mode 100644 wishlist_template/src/Entity/WishlistTemplateInterface.php create mode 100644 wishlist_template/src/Entity/WishlistTemplateType.php create mode 100644 wishlist_template/src/Entity/WishlistTemplateTypeInterface.php create mode 100644 wishlist_template/src/Event/WishlistTemplateEvent.php create mode 100644 wishlist_template/src/Event/WishlistTemplateEvents.php create mode 100644 wishlist_template/src/Form/WishlistTemplateForm.php create mode 100644 wishlist_template/src/Form/WishlistTemplateTypeDeleteForm.php create mode 100644 wishlist_template/src/Form/WishlistTemplateTypeForm.php create mode 100644 wishlist_template/src/Plugin/views/field/WishlistTemplate.php create mode 100644 wishlist_template/src/WishlistTemplateListBuilder.php create mode 100644 wishlist_template/src/WishlistTemplateStorage.php create mode 100644 wishlist_template/src/WishlistTemplateTypeListBuilder.php create mode 100644 wishlist_template/templates/wishlist-template.html.twig create mode 100644 wishlist_template/wishlist_template.info.yml create mode 100644 wishlist_template/wishlist_template.links.action.yml create mode 100644 wishlist_template/wishlist_template.links.menu.yml create mode 100644 wishlist_template/wishlist_template.links.task.yml create mode 100644 wishlist_template/wishlist_template.module create mode 100644 wishlist_template/wishlist_template.permissions.yml create mode 100644 wishlist_template/wishlist_template.routing.yml diff --git a/2791939-03.patch b/2791939-03.patch deleted file mode 100644 index 6a8239e..0000000 --- a/2791939-03.patch +++ /dev/null @@ -1,260 +0,0 @@ -diff --git a/commerce_wishlist.module b/commerce_wishlist.module -index c0f4ef8..75c95ee 100644 ---- a/commerce_wishlist.module -+++ b/commerce_wishlist.module -@@ -7,8 +7,10 @@ - - use Drupal\commerce_order\Entity\OrderInterface; - use Drupal\Core\Entity\EntityTypeInterface; -+use Drupal\Core\Entity\Entity\EntityViewDisplay; - use Drupal\Core\Field\BaseFieldDefinition; - use Drupal\Core\Form\FormStateInterface; -+use Drupal\Core\Link; - - /** - * Implements hook_entity_base_field_info(). -@@ -84,7 +86,7 @@ function commerce_wishlist_user_login($account) { - */ - function commerce_wishlist_commerce_order_delete(OrderInterface $order) { - if (!empty($order->wishlist)) { -- \Drupal::service('commerce_wishlist.wishlist_session')->deleteCartId($order->id()); -+ \Drupal::service('commerce_wishlist.wishlist_session')->deleteWishlistId($order->id()); - } - } - -@@ -192,3 +194,151 @@ function commerce_wishlist_order_type_form_submit($form, FormStateInterface $for - $order_type->setThirdPartySetting('commerce_wishlist', 'refresh_frequency', $settings['refresh_frequency']); - $order_type->save(); - } -+ -+/** -+ * Implements hook_field_formatter_settings_summary_alter(). -+ * -+ * Shows in the add-to-cart summary whether or not the wishlist is enabled. -+ */ -+function commerce_wishlist_field_formatter_settings_summary_alter(&$summary, $context) { -+ // Append a message to the summary when foo_formatter has -+ // my_setting set to TRUE for the current view mode. -+ if ($context['formatter']->getPluginId() == 'commerce_add_to_cart') { -+ if ($context['formatter']->getThirdPartySetting('commerce_wishlist', 'show_wishlist')) { -+ $summary[] = t('Wishlist enabled.'); -+ } else { -+ $summary[] = t('Wishlist disabled.'); -+ } -+ } -+} -+ -+/** -+ * Implements hook_field_formatter_third_party_settings_form(). -+ * -+ * Extends the add to cart formatter form with a show wishlist button. -+ */ -+function commerce_wishlist_field_formatter_third_party_settings_form($plugin, $field_definition, $view_mode, $form, $form_state) { -+ $element = array(); -+ -+ if ($plugin->getPluginId() == 'commerce_add_to_cart') { -+ $element['show_wishlist'] = array( -+ '#type' => 'checkbox', -+ '#title' => t('Show wishlist button'), -+ '#default_value' => $plugin->getThirdPartySetting('commerce_wishlist', 'show_wishlist'), -+ ); -+ $element['ajax_wishlist'] = array( -+ '#type' => 'checkbox', -+ '#title' => t('Display "Add to wishlist" as an ajax link'), -+ '#default_value' => $plugin->getThirdPartySetting('commerce_wishlist', 'ajax_wishlist'), -+ ); -+ $element['weight_wishlist'] = array( -+ '#type' => 'number', -+ '#title' => t('Change the weight of the wishlist button.'), -+ '#default_value' => $plugin->getThirdPartySetting('commerce_wishlist', 'weight_wishlist'), -+ ); -+ $element['label_wishlist'] = array( -+ '#type' => 'textfield', -+ '#title' => t('Override the wishlist button label'), -+ '#default_value' => $plugin->getThirdPartySetting('commerce_wishlist', 'label_wishlist'), -+ ); -+ } -+ return $element; -+} -+ -+/** -+ * Implements hook_form_FORM_ID_alter(). -+ */ -+function commerce_wishlist_form_commerce_line_item_product_variation_add_to_cart_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { -+ -+ // The product entity is thankfully available in the form storage. -+ $form_data = $form_state->getStorage(); -+ /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ -+ $product = $form_data['product']; -+ -+ // Grab the view mode third party settings. -+ $display = EntityViewDisplay::collectRenderDisplay($product, $form_data['view_mode']); -+ $settings = $display->get("content")['variations']['third_party_settings']['commerce_wishlist']; -+ -+ // Add the button -+ if ($settings['show_wishlist'] == 1) { -+ $label = ($settings['label_wishlist'] != "")? $settings['label_wishlist'] : t('Add to wishlist'); -+ $weight = ($settings['weight_wishlist'] != "")?$settings['weight_wishlist']:99; -+ if ($settings['ajax_wishlist'] != 1) { -+ $form['actions']['wishlist'] = array( -+ '#type' => 'submit', -+ '#value' => $label, -+ '#weight' => $weight, -+ '#submit' => array('commerce_wishlist_add_to_wishlist_form_submit'), -+ ); -+ } else { -+ $form['actions']['wishlist'] = array( -+ '#markup' => '' . $label . ' - Not Implemented Yet', -+ '#weight' => $weight, -+ ); -+ } -+ } -+} -+ -+/** -+ * Form submit handler for add-to-wishlist actions. -+ * -+ * Note that since we must fire this function off using a static form_alter call, -+ * we have no choice but to bring in the services and objects that we need. -+ * Normally we would create a class and use dependency injection to get at all -+ * of this context. We are very open to a better way of implmenting this hijack -+ * of the add to cart form. For now, it's a 1:1 copy from the core add to cart -+ * form ::submitForm process. -+ * -+ * @param array $form -+ * @param \Drupal\Core\Form\FormStateInterface $form_state -+ * @throws \Exception -+ */ -+function commerce_wishlist_add_to_wishlist_form_submit($form, FormStateInterface $form_state) { -+ -+ // Grab all objects that we need. -+ /** @var \Drupal\commerce_cart\Form\AddToCartForm $add_to_cart_form */ -+ /** @var \Drupal\commerce_order\Entity\LineItem $line_item */ -+ /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */ -+ $add_to_cart_form = $form_state->getFormObject(); -+ $line_item = $add_to_cart_form->buildEntity($form,$form_state); -+ $purchased_entity = $line_item->getPurchasedEntity(); -+ -+ // Grab all the services we need. -+ /** @var \Drupal\commerce_order\Resolver\DefaultOrderTypeResolver $order_resolver */ -+ /** @var \Drupal\commerce_store\StoreContext $store_context */ -+ /** @var \Drupal\commerce_wishlist\WishlistProvider $wishlist_provider */ -+ /** @var \Drupal\commerce_wishlist\WishlistManager $wishlist_manager */ -+ $order_resolver = \Drupal::service('commerce_order.default_order_type_resolver'); -+ $store_context = \Drupal::service('commerce_store.store_context'); -+ $wishlist_provider = \Drupal::service('commerce_wishlist.wishlist_provider'); -+ $wishlist_manager = \Drupal::service('commerce_wishlist.wishlist_manager'); -+ -+ // Determine the order type to use. -+ $order_type = $order_resolver->resolve($line_item); -+ -+ // Find a store to use. -+ $stores = $purchased_entity->getStores(); -+ if (count($stores) === 1) { -+ $store = reset($stores); -+ } -+ else { -+ $store = $store_context->getStore(); -+ if (!in_array($store, $stores)) { -+ // Indicates that the site listings are not filtered properly. -+ throw new \Exception("The given entity can't be purchased from the current store."); -+ } -+ } -+ -+ // Use existing or create a new wishlist. -+ $wishlist = $wishlist_provider->getWishlist($order_type, $store); -+ if (!$wishlist) { -+ $wishlist = $wishlist_provider->createWishlist($order_type, $store); -+ } -+ $wishlist_manager->addLineItem($wishlist, $line_item, $form_state->get(['settings', 'combine'])); -+ -+ // Let people know about the outcome. -+ drupal_set_message(t('@entity added to @wishlist-link.', [ -+ '@entity' => $purchased_entity->label(), -+ '@wishlist-link' => Link::createFromRoute(t('your wishlist', [], ['context' => 'wishlist link']), 'commerce_wishlist.page')->toString(), -+ ])); -+} -diff --git a/config/schema/commerce_wishlist.schema.yml b/config/schema/commerce_wishlist.schema.yml -index 8b27bb7..2dffbf1 100644 ---- a/config/schema/commerce_wishlist.schema.yml -+++ b/config/schema/commerce_wishlist.schema.yml -@@ -3,11 +3,27 @@ commerce_order.commerce_order_type.*.third_party.commerce_wishlist: - label: 'Wishlist settings' - mapping: - wishlist_form_view: -- type: string -- label: 'Wishlist form view' -+ type: string -+ label: 'Wishlist form view' - refresh_mode: -- type: string -- label: 'Wishlist refresh mode' -+ type: string -+ label: 'Wishlist refresh mode' - refresh_frequency: -- type: integer -- label: 'Wishlist refresh frequency' -+ type: integer -+ label: 'Wishlist refresh frequency' -+commerce_product.commerce_product_type.*.third_party.commerce_wishlist: -+ type: mapping -+ label: 'Wishlist settings' -+ mapping: -+ show_wishlist: -+ type: boolean -+ label: 'Show wishlist button' -+ ajax_wishlist: -+ type: boolean -+ label: 'Render wishlist button as ajax link' -+ weight_wishlist: -+ type: integer -+ label: 'Modify wishlist button sort order' -+ label_wishlist: -+ type: text -+ label: 'Override the button label' -diff --git a/src/Event/WishlistAddEvent.php b/src/Event/WishlistEntityAddEvent.php -similarity index 100% -rename from src/Event/WishlistAddEvent.php -rename to src/Event/WishlistEntityAddEvent.php -diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php -index deb1208..b1fa4d6 100644 ---- a/src/Exception/ExceptionInterface.php -+++ b/src/Exception/ExceptionInterface.php -@@ -5,4 +5,4 @@ namespace Drupal\commerce_wishlist\Exception; - /** - * Exception interface for all exceptions thrown by the Wishlist module. - */ --interface ExceptionInterface {} -\ No newline at end of file -+interface ExceptionInterface {} -diff --git a/src/WishlistManager.php b/src/WishlistManager.php -index 88fdd55..41dcda9 100644 ---- a/src/WishlistManager.php -+++ b/src/WishlistManager.php -@@ -10,6 +10,7 @@ use Drupal\commerce_wishlist\Event\WishlistEmptyEvent; - use Drupal\commerce_wishlist\Event\WishlistEntityAddEvent; - use Drupal\commerce_wishlist\Event\WishlistLineItemRemoveEvent; - use Drupal\commerce_wishlist\Event\WishlistLineItemUpdateEvent; -+use Drupal\commerce_price\Calculator; - use Drupal\Core\Entity\EntityTypeManagerInterface; - use Symfony\Component\EventDispatcher\EventDispatcherInterface; - -@@ -108,7 +109,7 @@ class WishlistManager implements WishlistManagerInterface { - } - $needs_wishlist_save = FALSE; - if ($matching_line_item) { -- $new_quantity = $matching_line_item->getQuantity() + $quantity; -+ $new_quantity = Calculator::add($matching_line_item->getQuantity(), $quantity); - $matching_line_item->setQuantity($new_quantity); - $matching_line_item->save(); - } -@@ -118,7 +119,8 @@ class WishlistManager implements WishlistManagerInterface { - $needs_wishlist_save = TRUE; - } - -- $event = new WishlistEntityAddEvent($wishlist, $purchased_entity, $quantity, $line_item); -+ // @todo: figure out why this produces a fatal error... -+ // $event = new WishlistEntityAddEvent($wishlist, $purchased_entity, $quantity, $line_item); - $this->eventDispatcher->dispatch(WishlistEvents::WISHLIST_ENTITY_ADD, $event); - if ($needs_wishlist_save && $save_wishlist) { - $wishlist->save(); diff --git a/templates/wishlist_template.info.yml b/templates/wishlist_template.info.yml deleted file mode 100644 index 08d262f..0000000 --- a/templates/wishlist_template.info.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: Commerce Wishlist Template -type: module -description: Provides the ability to create product category lists that change the way a wishlist is created and presented. -package: Commerce (contrib) -core: 8.x -dependencies: - - commerce_wishlist diff --git a/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml b/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml new file mode 100644 index 0000000..d58d007 --- /dev/null +++ b/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml @@ -0,0 +1,16 @@ +langcode: en +status: true +dependencies: + config: + - wishlist_template.wishlist_template_type.default + module: + - address +id: wishlist_template.default.default +targetEntityType: wishlist_template +bundle: default +mode: default +content: + name: + type: true +hidden: + name: true diff --git a/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml b/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml new file mode 100644 index 0000000..7d477e7 --- /dev/null +++ b/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml @@ -0,0 +1,6 @@ +langcode: en +status: true +dependencies: { } +id: default +label: Default +description: null diff --git a/wishlist_template/config/schema/wishlist_template.schema.yml b/wishlist_template/config/schema/wishlist_template.schema.yml new file mode 100644 index 0000000..6c6a0db --- /dev/null +++ b/wishlist_template/config/schema/wishlist_template.schema.yml @@ -0,0 +1,22 @@ +wishlist_template.wishlist_template_type.*: + type: config_entity + label: 'Wishlist Tempalte type config' + mapping: + id: + type: string + label: 'Type' + label: + type: label + label: 'Label' + uuid: + type: string + description: + type: text + label: 'Description' + +views.field.wishlist_template: + type: views.field.field + mapping: + hide_single_wishlist_template: + type: boolean + label: 'Hide if there''s only one wishlist template.' diff --git a/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php b/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php new file mode 100644 index 0000000..dedde55 --- /dev/null +++ b/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php @@ -0,0 +1,46 @@ +get('name')->value; + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getOwner() { + return $this->get('uid')->entity; + } + + /** + * {@inheritdoc} + */ + public function setOwner(UserInterface $account) { + $this->set('uid', $account->id()); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getOwnerId() { + return $this->get('uid')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setOwnerId($uid) { + $this->set('uid', $uid); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getTaxonomyTermViewMode() { + return $this->get('taxonomy_term_view_mode')->value; + } + + /** + * {@inheritdoc} + */ + public function setTaxonomyTermViewMode($taxonomy_term_view_mode) { + $this->set('taxonomy_term_view_mode', $taxonomy_term_view_mode); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getTerms() { + return $this->get('terms')->value; + } + + /** + * {@inheritdoc} + */ + public function setTerms($terms) { + $this->set('terms', $terms); + return $this; + } + + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields = parent::baseFieldDefinitions($entity_type); + + $fields['type'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Type')) + ->setDescription(t('The wishlist template type.')) + ->setSetting('target_type', 'wishlist_template_type') + ->setReadOnly(TRUE); + + $fields['uid'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Owner')) + ->setDescription(t('The store owner.')) + ->setDefaultValueCallback('Drupal\commerce_store\Entity\Store::getCurrentUserId') + ->setSetting('target_type', 'user') + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 50, + ]); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The wishlist template name.')) + ->setRequired(TRUE) + ->setTranslatable(TRUE) + ->setSettings([ + 'default_value' => '', + 'max_length' => 255, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 0, + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + + $fields['taxonomy_term_view_mode'] = BaseFieldDefinition::create('list_string') + ->setLabel(t('Category view mode')) + ->setDescription(t('The view mode to be used when rendering each category in the wishlist template.')) + ->setRequired(TRUE) + ->setTranslatable(TRUE) + ->setSettings([ + 'allowed_values_function' => '_wishlist_template_term_view_mode_values', + 'multiple' => false, + ]) + ->setDisplayOptions('form', [ + 'type' => 'select', + 'weight' => 0, + ]) + ->setDisplayOptions('view', [ + 'type' => 'hidden', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + + $fields['terms'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Categories')) + ->setDescription(t('The categories used for grouping products the creating the wishlist template.')) + ->setSetting('target_type', 'taxonomy_term') + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 50, + ]); + + return $fields; + } + + /** + * Default value callback for 'uid' base field definition. + * + * @see ::baseFieldDefinitions() + * + * @return array + * An array of default values. + */ + public static function getCurrentUserId() { + return [\Drupal::currentUser()->id()]; + } +} diff --git a/wishlist_template/src/Entity/WishlistTemplateInterface.php b/wishlist_template/src/Entity/WishlistTemplateInterface.php new file mode 100644 index 0000000..b29419d --- /dev/null +++ b/wishlist_template/src/Entity/WishlistTemplateInterface.php @@ -0,0 +1,67 @@ +description; + } + + /** + * {@inheritdoc} + */ + public function setDescription($description) { + $this->description = $description; + return $this; + } + +} diff --git a/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php b/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php new file mode 100644 index 0000000..b873802 --- /dev/null +++ b/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php @@ -0,0 +1,12 @@ +wishlist_template = $wishlist; + } + + /** + * Gets the wishlist template. + * + * @return \Drupal\wishlist_template\Entity\WishlistTemplateInterface + * The wishlist template. + */ + public function getWishlisttemplate() { + return $this->wishlist_template; + } + +} diff --git a/wishlist_template/src/Event/WishlistTemplateEvents.php b/wishlist_template/src/Event/WishlistTemplateEvents.php new file mode 100644 index 0000000..37f1076 --- /dev/null +++ b/wishlist_template/src/Event/WishlistTemplateEvents.php @@ -0,0 +1,90 @@ +entity; + + return $form; + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::save(). + */ + public function save(array $form, FormStateInterface $form_state) { + $this->entity->save(); + drupal_set_message($this->t('Saved the %label wishlist template.', [ + '%label' => $this->entity->label(), + ])); + $form_state->setRedirect('entity.wishlist_template.collection'); + } + +} diff --git a/wishlist_template/src/Form/WishlistTemplateTypeDeleteForm.php b/wishlist_template/src/Form/WishlistTemplateTypeDeleteForm.php new file mode 100644 index 0000000..fe5890a --- /dev/null +++ b/wishlist_template/src/Form/WishlistTemplateTypeDeleteForm.php @@ -0,0 +1,59 @@ +queryFactory = $query_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.query') + ); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $wishlist_template_count = $this->queryFactory->get('wishlist_template') + ->condition('type', $this->entity->id()) + ->count() + ->execute(); + if ($wishlist_template_count) { + $caption = '

' . $this->formatPlural($wishlist_template_count, '%type is used by 1 wishlist template on your site. You can not remove this wishlist template type until you have removed all of the %type wishlist templates.', '%type is used by @count wishlist templates on your site. You may not remove %type until you have removed all of the %type wishlist templates.', ['%type' => $this->entity->label()]) . '

'; + $form['#title'] = $this->getQuestion(); + $form['description'] = ['#markup' => $caption]; + return $form; + } + + return parent::buildForm($form, $form_state); + } + +} diff --git a/wishlist_template/src/Form/WishlistTemplateTypeForm.php b/wishlist_template/src/Form/WishlistTemplateTypeForm.php new file mode 100644 index 0000000..118c5d1 --- /dev/null +++ b/wishlist_template/src/Form/WishlistTemplateTypeForm.php @@ -0,0 +1,73 @@ +entity; + + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $wishlist_template_type->label(), + '#description' => $this->t('Label for the wishlist template type.'), + '#required' => TRUE, + ]; + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $wishlist_template_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\wishlist_template\Entity\WishlistTemplateType::load', + ], + '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, + ]; + $form['description'] = [ + '#type' => 'textfield', + '#title' => $this->t('Description'), + '#default_value' => $wishlist_template_type->getDescription(), + ]; + + if ($this->moduleHandler->moduleExists('language')) { + $form['language'] = [ + '#type' => 'details', + '#title' => $this->t('Language settings'), + '#group' => 'additional_settings', + ]; + $form['language']['language_configuration'] = [ + '#type' => 'language_configuration', + '#entity_information' => [ + 'entity_type' => 'wishlist_template', + 'bundle' => $wishlist_template_type->id(), + ], + '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('wishlist_template', $wishlist_template_type->id()), + ]; + $form['#submit'][] = 'language_configuration_element_submit'; + } + + return $this->protectBundleIdElement($form); + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $this->entity->save(); + drupal_set_message($this->t('Saved the %label wishlist template type.', [ + '%label' => $this->entity->label(), + ])); + $form_state->setRedirect('entity.wishlist_template_type.collection'); + } + +} diff --git a/wishlist_template/src/Plugin/views/field/WishlistTemplate.php b/wishlist_template/src/Plugin/views/field/WishlistTemplate.php new file mode 100644 index 0000000..c79927b --- /dev/null +++ b/wishlist_template/src/Plugin/views/field/WishlistTemplate.php @@ -0,0 +1,56 @@ + TRUE]; + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + + $form['hide_single_wishlist_template'] = [ + '#type' => 'checkbox', + '#title' => t("Hide if there's only one wishlist template."), + '#default_value' => $this->options['hide_single_wishlist_template'], + ]; + } + + /** + * {@inheritdoc} + */ + public function access(AccountInterface $account) { + $wishlist_template_query = $this->entityManager->getStorage('wishlist_template')->getQuery(); + $wishlist_template_count = $wishlist_template_query->count()->execute(); + if ($this->options['hide_single_wishlist_template'] && $wishlist_template_count <= 1) { + return FALSE; + } + + return parent::access($account); + } + +} diff --git a/wishlist_template/src/WishlistTemplateListBuilder.php b/wishlist_template/src/WishlistTemplateListBuilder.php new file mode 100644 index 0000000..3d9b2ea --- /dev/null +++ b/wishlist_template/src/WishlistTemplateListBuilder.php @@ -0,0 +1,39 @@ +bundle()); + + $row['name']['data'] = [ + '#type' => 'link', + '#title' => $entity->label(), + ] + $entity->toUrl()->toRenderArray(); + $row['type'] = $wishlist_template_type->label(); + + return $row + parent::buildRow($entity); + } + +} diff --git a/wishlist_template/src/WishlistTemplateStorage.php b/wishlist_template/src/WishlistTemplateStorage.php new file mode 100644 index 0000000..a1342c4 --- /dev/null +++ b/wishlist_template/src/WishlistTemplateStorage.php @@ -0,0 +1,66 @@ +configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) { + return new static( + $entityType, + $container->get('database'), + $container->get('entity.manager'), + $container->get('cache.entity'), + $container->get('language_manager'), + $container->get('event_dispatcher'), + $container->get('config.factory') + ); + } + +} diff --git a/wishlist_template/src/WishlistTemplateTypeListBuilder.php b/wishlist_template/src/WishlistTemplateTypeListBuilder.php new file mode 100644 index 0000000..10d5330 --- /dev/null +++ b/wishlist_template/src/WishlistTemplateTypeListBuilder.php @@ -0,0 +1,31 @@ +t('Wishlist Template type'); + $header['id'] = $this->t('Machine name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $entity->label(); + $row['id'] = $entity->id(); + return $row + parent::buildRow($entity); + } + +} diff --git a/wishlist_template/templates/wishlist-template.html.twig b/wishlist_template/templates/wishlist-template.html.twig new file mode 100644 index 0000000..303c8f0 --- /dev/null +++ b/wishlist_template/templates/wishlist-template.html.twig @@ -0,0 +1,24 @@ +{# +/** + * @file + * + * Default template for wishlist templates. + * + * Available variables: + * - attributes: HTML attributes for the wrapper. + * - wishlist_template: The rendered wishlist template fields. + * Use 'wishlist_template' to print them all, or print a subset such as + * 'wishlist_template.name'. Use the following code to exclude the + * printing of a given field: + * @code + * {{ wishlist_template|without('name') }} + * @endcode + * - wishlist_template_entity: The wishlist template entity. + * - wishlist_template_url: The wishlist template URL. + * + * @ingroup themeable + */ +#} + + {{ wishlist_template }} + diff --git a/wishlist_template/wishlist_template.info.yml b/wishlist_template/wishlist_template.info.yml new file mode 100644 index 0000000..a7db265 --- /dev/null +++ b/wishlist_template/wishlist_template.info.yml @@ -0,0 +1,15 @@ +name: Commerce Wishlist Template +type: module +description: Provides the ability to create product category lists that change the way a wishlist is created and presented. +package: Commerce (contrib) +core: 8.x +dependencies: + - commerce_wishlist + - entity_reference +config_devel: + install: + - wishlist_template.wishlist_template_type.default + - wishlist_template.settings + - core.entity_view_display.wishlist_template.default.default + - views.view.wishlist_templates + - system.action.commerce_wishlist_delete_templates_action diff --git a/wishlist_template/wishlist_template.links.action.yml b/wishlist_template/wishlist_template.links.action.yml new file mode 100644 index 0000000..d3f1d78 --- /dev/null +++ b/wishlist_template/wishlist_template.links.action.yml @@ -0,0 +1,11 @@ +entity.wishlist_template.add_page: + route_name: entity.wishlist_template.add_page + title: 'Add wishlist template' + appears_on: + - entity.wishlist_template.collection + +entity.wishlist_template_type.add_form: + route_name: entity.wishlist_template_type.add_form + title: 'Add wishlist template type' + appears_on: + - entity.wishlist_template_type.collection \ No newline at end of file diff --git a/wishlist_template/wishlist_template.links.menu.yml b/wishlist_template/wishlist_template.links.menu.yml new file mode 100644 index 0000000..bccb440 --- /dev/null +++ b/wishlist_template/wishlist_template.links.menu.yml @@ -0,0 +1,11 @@ +entity.wishlist_template.collection: + title: 'Wishlist template' + route_name: 'entity.wishlist_template.collection' + parent: 'commerce.admin_commerce' + description: 'Manage your wishlist templates.' + +entity.wishlist_template_type.collection: + title: 'Wishlist template types' + route_name: 'entity.wishlist_template_type.collection' + parent: 'commerce.configuration' + description: 'Manage your wishlist template types.' diff --git a/wishlist_template/wishlist_template.links.task.yml b/wishlist_template/wishlist_template.links.task.yml new file mode 100644 index 0000000..95d7a57 --- /dev/null +++ b/wishlist_template/wishlist_template.links.task.yml @@ -0,0 +1,24 @@ +entity.wishlist_template.canonical: + route_name: entity.wishlist_template.canonical + base_route: entity.wishlist_template.canonical + title: View + +entity.wishlist_template.collection: + route_name: entity.wishlist_template.collection + base_route: entity.wishlist_template.collection + title: List + +entity.wishlist_template.edit_form: + route_name: entity.wishlist_template.edit_form + base_route: entity.wishlist_template.edit_form + title: Edit + +entity.wishlist_template_type.collection: + route_name: entity.wishlist_template_type.collection + route_name: entity.wishlist_template_type.collection + title: List + +entity.wishlist_template_type.edit_form: + route_name: entity.wishlist_template_type.edit_form + base_route: entity.wishlist_template_type.edit_form + title: Edit diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module new file mode 100644 index 0000000..7f1b792 --- /dev/null +++ b/wishlist_template/wishlist_template.module @@ -0,0 +1,70 @@ +getViewModes($entity_type_id); + + // Create a simple "allowed_values" array + $allowed_values = array(); + foreach ($view_modes as $id => $view_mode) { + $allowed_values[$id] = $view_mode['label'] . ' (' . $id . ')'; + } + + return $allowed_values; +} + +/** + * Implements hook_theme(). + */ +function wishlist_template_theme() { + return [ + 'wishlist_template' => [ + 'render element' => 'elements', + ], + ]; +} + +/** + * Implements hook_theme_suggestions_wishlist_template(). + */ +function wishlist_template_theme_suggestions_wishlist_template(array $variables) { + return _commerce_entity_theme_suggestions('wishlist_template', $variables); +} + +/** + * Prepares variables for wishlist template templates. + * + * Default template: wishlist-template.html.twig. + * + * @param array $variables + * An associative array containing: + * - elements: An associative array containing rendered fields. + * - attributes: HTML attributes for the containing element. + */ +function template_preprocess_wishlist_template(array &$variables) { + /** @var Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template */ + $wishlist_template = $variables['elements']['#wishlist_template']; + + $variables['wishlist_template_entity'] = $wishlist_template; + $variables['wishlist_template_url'] = $wishlist_template->toUrl(); + $variables['wishlist_template'] = []; + foreach (Element::children($variables['elements']) as $key) { + $variables['wishlist_template'][$key] = $variables['elements'][$key]; + } +} diff --git a/wishlist_template/wishlist_template.permissions.yml b/wishlist_template/wishlist_template.permissions.yml new file mode 100644 index 0000000..d4337bb --- /dev/null +++ b/wishlist_template/wishlist_template.permissions.yml @@ -0,0 +1,9 @@ +'administer wishlist templates': + title: 'Administer wishlist templates' + description: 'Allows users to create, update, and delete wishlist templates.' + 'restrict access': TRUE + +'administer wishlist template types': + title: 'Administer wishlist template types' + description: 'Allows users to create, update, and delete wishlist template types.' + 'restrict access': TRUE diff --git a/wishlist_template/wishlist_template.routing.yml b/wishlist_template/wishlist_template.routing.yml new file mode 100644 index 0000000..ea44fdc --- /dev/null +++ b/wishlist_template/wishlist_template.routing.yml @@ -0,0 +1,15 @@ +entity.wishlist_template.collection: + path: '/admin/commerce/wishlist-templates' + defaults: + _entity_list: 'wishlist_template' + _title: 'Wishlist template' + requirements: + _permission: 'administer wishlist templates' + +entity.wishlist_template_type.collection: + path: '/admin/commerce/config/wishlist-template-types' + defaults: + _entity_list: 'wishlist_template_type' + _title: 'Wishlist template types' + requirements: + _permission: 'administer wishlist template types' From 17788b383a53d8ce994765052a3a7f6663fd6ec1 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Mon, 5 Sep 2016 17:02:45 -0400 Subject: [PATCH 03/12] WIP Issue #2794531: Wishlist Templates - Still not installing --- ...action.wishlist_template_delete_action.yml | 13 + .../install/views.view.wishlist_templates.yml | 434 ++++++++++++++++++ wishlist_template/wishlist_template.info.yml | 6 +- 3 files changed, 450 insertions(+), 3 deletions(-) create mode 100644 wishlist_template/config/install/system.action.wishlist_template_delete_action.yml create mode 100644 wishlist_template/config/install/views.view.wishlist_templates.yml diff --git a/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml b/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml new file mode 100644 index 0000000..59936da --- /dev/null +++ b/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml @@ -0,0 +1,13 @@ +langcode: en +status: true +dependencies: + enforced: + module: + - commerce_store + module: + - entity +id: wishlist_template_delete_action +label: 'Delete wishlist template' +type: commerce_store +plugin: 'entity_delete_action:commerce_wishlist' +configuration: { } diff --git a/wishlist_template/config/install/views.view.wishlist_templates.yml b/wishlist_template/config/install/views.view.wishlist_templates.yml new file mode 100644 index 0000000..3f9551f --- /dev/null +++ b/wishlist_template/config/install/views.view.wishlist_templates.yml @@ -0,0 +1,434 @@ +langcode: en +status: true +dependencies: + module: + - commerce + - wishlist_template + - system + - user +id: wishlist_templates +label: Stores +module: views +description: '' +tag: Commerce +base_table: wishlist_template_field_data +base_field: wishlist_template_id +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: perm + options: + perm: 'administer wishlist templates' + cache: + type: none + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: full + options: + items_per_page: 50 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: '‹ previous' + next: 'next ›' + first: '« first' + last: 'last »' + quantity: 9 + style: + type: table + options: + grouping: { } + row_class: '' + default_row_class: true + override: true + sticky: false + caption: '' + summary: '' + description: '' + columns: + name: name + type: type + operations: operations + info: + name: + sortable: true + default_sort_order: asc + align: '' + separator: '' + empty_column: false + responsive: '' + type: + sortable: true + default_sort_order: asc + align: '' + separator: '' + empty_column: true + responsive: '' + operations: + sortable: false + default_sort_order: asc + align: '' + separator: '' + empty_column: false + responsive: '' + default: '-1' + empty_table: true + row: + type: fields + fields: + wishlist_template_bulk_form: + id: wishlist_template_bulk_form + table: wishlist_template + field: wishlist_template_bulk_form + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + action_title: 'With selection' + include_exclude: exclude + selected_actions: { } + entity_type: wishlist_template + plugin_id: bulk_form + name: + id: name + table: wishlist_template_field_data + field: name + relationship: none + group_type: group + admin_label: '' + label: Name + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: string + settings: + link_to_entity: false + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: null + entity_field: name + plugin_id: field + type: + id: type + table: wishlist_template_field_data + field: type + relationship: none + group_type: group + admin_label: '' + label: Type + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: target_id + type: entity_reference_label + settings: + link: false + group_column: target_id + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + hide_single_bundle: true + entity_type: wishlist_template + entity_field: type + plugin_id: commerce_entity_bundle + operations: + id: operations + table: wishlist_template + field: operations + relationship: none + group_type: group + admin_label: '' + label: Operations + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + destination: false + entity_type: wishlist_template + plugin_id: entity_operations + filters: + type: + id: type + table: wishlist_template_field_data + field: type + relationship: none + group_type: group + admin_label: '' + operator: in + value: { } + group: 1 + exposed: true + expose: + operator_id: type_op + label: Type + description: '' + use_operator: false + operator: type_op + identifier: type + required: false + remember: false + multiple: false + remember_roles: + authenticated: authenticated + anonymous: '0' + administrator: '0' + reduce: false + hide_single_bundle: true + is_grouped: false + group_info: + label: '' + description: '' + identifier: '' + optional: true + widget: select + multiple: false + remember: false + default_group: All + default_group_multiple: { } + group_items: { } + entity_type: wishlist_template + entity_field: type + plugin_id: commerce_entity_bundle + sorts: { } + title: Stores + header: { } + footer: { } + empty: + area_text_custom: + id: area_text_custom + table: views + field: area_text_custom + relationship: none + group_type: group + admin_label: '' + empty: true + tokenize: false + content: 'No wishlist templates available.' + plugin_id: text_custom + relationships: { } + arguments: { } + display_extenders: { } + cache_metadata: + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - url.query_args + - user.permissions + cacheable: false + max-age: 0 + tags: { } + page_1: + display_plugin: page + id: page_1 + display_title: Page + position: 1 + display_options: + display_extenders: { } + path: admin/commerce/wishlist_templates + cache_metadata: + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - url.query_args + - user.permissions + cacheable: false + max-age: 0 + tags: { } diff --git a/wishlist_template/wishlist_template.info.yml b/wishlist_template/wishlist_template.info.yml index a7db265..6bc2354 100644 --- a/wishlist_template/wishlist_template.info.yml +++ b/wishlist_template/wishlist_template.info.yml @@ -6,10 +6,10 @@ core: 8.x dependencies: - commerce_wishlist - entity_reference + - options config_devel: install: - wishlist_template.wishlist_template_type.default - - wishlist_template.settings - core.entity_view_display.wishlist_template.default.default - - views.view.wishlist_templates - - system.action.commerce_wishlist_delete_templates_action + - views.view.wishlist_template + - system.action.wishlist_template_delete_action From d8c03b76ef7d49acdd8d928a5870e4b269b4d407 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Tue, 6 Sep 2016 16:51:35 -0400 Subject: [PATCH 04/12] WIP Issue #2794531: Wishlist Templates - Starting over with console generated content --- ...play.wishlist_template.default.default.yml | 16 - ...action.wishlist_template_delete_action.yml | 13 - .../install/views.view.wishlist_templates.yml | 434 ------------------ ...emplate.wishlist_template_type.default.yml | 6 - .../schema/wishlist_template.schema.yml | 22 - .../schema/wishlist_template_type.schema.yml | 12 + .../EntityWishlistTemplateInterface.php | 46 -- .../src/Entity/WishlistTemplate.php | 203 +++++--- .../src/Entity/WishlistTemplateInterface.php | 70 ++- .../src/Entity/WishlistTemplateType.php | 55 +-- .../Entity/WishlistTemplateTypeInterface.php | 7 +- .../src/Entity/WishlistTemplateViewsData.php | 28 ++ .../src/Event/WishlistTemplateEvent.php | 42 -- .../src/Event/WishlistTemplateEvents.php | 90 ---- .../src/Form/WishlistTemplateDeleteForm.php | 15 + .../src/Form/WishlistTemplateForm.php | 35 +- .../src/Form/WishlistTemplateSettingsForm.php | 55 +++ .../Form/WishlistTemplateTypeDeleteForm.php | 58 ++- .../src/Form/WishlistTemplateTypeForm.php | 64 ++- .../Plugin/views/field/WishlistTemplate.php | 56 --- .../WishlistTemplateAccessControlHandler.php | 47 ++ .../src/WishlistTemplateHtmlRouteProvider.php | 85 ++++ .../src/WishlistTemplateListBuilder.php | 32 +- .../src/WishlistTemplateStorage.php | 66 --- .../WishlistTemplateTypeHtmlRouteProvider.php | 59 +++ .../src/WishlistTemplateTypeListBuilder.php | 5 +- ...shlist-template-content-add-list.html.twig | 23 + .../templates/wishlist-template.html.twig | 24 - .../templates/wishlist_template.html.twig | 22 + wishlist_template/wishlist_template.info.yml | 7 +- .../wishlist_template.links.action.yml | 14 +- .../wishlist_template.links.menu.yml | 17 +- .../wishlist_template.links.task.yml | 24 +- wishlist_template/wishlist_template.module | 92 ++-- wishlist_template/wishlist_template.page.inc | 32 ++ .../wishlist_template.permissions.yml | 31 +- .../wishlist_template.routing.yml | 15 - 37 files changed, 787 insertions(+), 1135 deletions(-) delete mode 100644 wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml delete mode 100644 wishlist_template/config/install/system.action.wishlist_template_delete_action.yml delete mode 100644 wishlist_template/config/install/views.view.wishlist_templates.yml delete mode 100644 wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml delete mode 100644 wishlist_template/config/schema/wishlist_template.schema.yml create mode 100644 wishlist_template/config/schema/wishlist_template_type.schema.yml delete mode 100644 wishlist_template/src/Entity/EntityWishlistTemplateInterface.php create mode 100644 wishlist_template/src/Entity/WishlistTemplateViewsData.php delete mode 100644 wishlist_template/src/Event/WishlistTemplateEvent.php delete mode 100644 wishlist_template/src/Event/WishlistTemplateEvents.php create mode 100644 wishlist_template/src/Form/WishlistTemplateDeleteForm.php create mode 100644 wishlist_template/src/Form/WishlistTemplateSettingsForm.php delete mode 100644 wishlist_template/src/Plugin/views/field/WishlistTemplate.php create mode 100644 wishlist_template/src/WishlistTemplateAccessControlHandler.php create mode 100644 wishlist_template/src/WishlistTemplateHtmlRouteProvider.php delete mode 100644 wishlist_template/src/WishlistTemplateStorage.php create mode 100644 wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php create mode 100644 wishlist_template/templates/wishlist-template-content-add-list.html.twig delete mode 100644 wishlist_template/templates/wishlist-template.html.twig create mode 100644 wishlist_template/templates/wishlist_template.html.twig create mode 100644 wishlist_template/wishlist_template.page.inc delete mode 100644 wishlist_template/wishlist_template.routing.yml diff --git a/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml b/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml deleted file mode 100644 index d58d007..0000000 --- a/wishlist_template/config/install/core.entity_view_display.wishlist_template.default.default.yml +++ /dev/null @@ -1,16 +0,0 @@ -langcode: en -status: true -dependencies: - config: - - wishlist_template.wishlist_template_type.default - module: - - address -id: wishlist_template.default.default -targetEntityType: wishlist_template -bundle: default -mode: default -content: - name: - type: true -hidden: - name: true diff --git a/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml b/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml deleted file mode 100644 index 59936da..0000000 --- a/wishlist_template/config/install/system.action.wishlist_template_delete_action.yml +++ /dev/null @@ -1,13 +0,0 @@ -langcode: en -status: true -dependencies: - enforced: - module: - - commerce_store - module: - - entity -id: wishlist_template_delete_action -label: 'Delete wishlist template' -type: commerce_store -plugin: 'entity_delete_action:commerce_wishlist' -configuration: { } diff --git a/wishlist_template/config/install/views.view.wishlist_templates.yml b/wishlist_template/config/install/views.view.wishlist_templates.yml deleted file mode 100644 index 3f9551f..0000000 --- a/wishlist_template/config/install/views.view.wishlist_templates.yml +++ /dev/null @@ -1,434 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - commerce - - wishlist_template - - system - - user -id: wishlist_templates -label: Stores -module: views -description: '' -tag: Commerce -base_table: wishlist_template_field_data -base_field: wishlist_template_id -core: 8.x -display: - default: - display_plugin: default - id: default - display_title: Master - position: 0 - display_options: - access: - type: perm - options: - perm: 'administer wishlist templates' - cache: - type: none - options: { } - query: - type: views_query - options: - disable_sql_rewrite: false - distinct: false - replica: false - query_comment: '' - query_tags: { } - exposed_form: - type: basic - options: - submit_button: Apply - reset_button: false - reset_button_label: Reset - exposed_sorts_label: 'Sort by' - expose_sort_order: true - sort_asc_label: Asc - sort_desc_label: Desc - pager: - type: full - options: - items_per_page: 50 - offset: 0 - id: 0 - total_pages: null - expose: - items_per_page: false - items_per_page_label: 'Items per page' - items_per_page_options: '5, 10, 25, 50' - items_per_page_options_all: false - items_per_page_options_all_label: '- All -' - offset: false - offset_label: Offset - tags: - previous: '‹ previous' - next: 'next ›' - first: '« first' - last: 'last »' - quantity: 9 - style: - type: table - options: - grouping: { } - row_class: '' - default_row_class: true - override: true - sticky: false - caption: '' - summary: '' - description: '' - columns: - name: name - type: type - operations: operations - info: - name: - sortable: true - default_sort_order: asc - align: '' - separator: '' - empty_column: false - responsive: '' - type: - sortable: true - default_sort_order: asc - align: '' - separator: '' - empty_column: true - responsive: '' - operations: - sortable: false - default_sort_order: asc - align: '' - separator: '' - empty_column: false - responsive: '' - default: '-1' - empty_table: true - row: - type: fields - fields: - wishlist_template_bulk_form: - id: wishlist_template_bulk_form - table: wishlist_template - field: wishlist_template_bulk_form - relationship: none - group_type: group - admin_label: '' - label: '' - exclude: false - alter: - alter_text: false - text: '' - make_link: false - path: '' - absolute: false - external: false - replace_spaces: false - path_case: none - trim_whitespace: false - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: false - max_length: 0 - word_boundary: true - ellipsis: true - more_link: false - more_link_text: '' - more_link_path: '' - strip_tags: false - trim: false - preserve_tags: '' - html: false - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: false - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: true - empty: '' - hide_empty: false - empty_zero: false - hide_alter_empty: true - action_title: 'With selection' - include_exclude: exclude - selected_actions: { } - entity_type: wishlist_template - plugin_id: bulk_form - name: - id: name - table: wishlist_template_field_data - field: name - relationship: none - group_type: group - admin_label: '' - label: Name - exclude: false - alter: - alter_text: false - text: '' - make_link: false - path: '' - absolute: false - external: false - replace_spaces: false - path_case: none - trim_whitespace: false - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: false - max_length: 0 - word_boundary: true - ellipsis: true - more_link: false - more_link_text: '' - more_link_path: '' - strip_tags: false - trim: false - preserve_tags: '' - html: false - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: true - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: true - empty: '' - hide_empty: false - empty_zero: false - hide_alter_empty: true - click_sort_column: value - type: string - settings: - link_to_entity: false - group_column: value - group_columns: { } - group_rows: true - delta_limit: 0 - delta_offset: 0 - delta_reversed: false - delta_first_last: false - multi_type: separator - separator: ', ' - field_api_classes: false - entity_type: null - entity_field: name - plugin_id: field - type: - id: type - table: wishlist_template_field_data - field: type - relationship: none - group_type: group - admin_label: '' - label: Type - exclude: false - alter: - alter_text: false - text: '' - make_link: false - path: '' - absolute: false - external: false - replace_spaces: false - path_case: none - trim_whitespace: false - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: false - max_length: 0 - word_boundary: true - ellipsis: true - more_link: false - more_link_text: '' - more_link_path: '' - strip_tags: false - trim: false - preserve_tags: '' - html: false - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: true - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: true - empty: '' - hide_empty: false - empty_zero: false - hide_alter_empty: true - click_sort_column: target_id - type: entity_reference_label - settings: - link: false - group_column: target_id - group_columns: { } - group_rows: true - delta_limit: 0 - delta_offset: 0 - delta_reversed: false - delta_first_last: false - multi_type: separator - separator: ', ' - field_api_classes: false - hide_single_bundle: true - entity_type: wishlist_template - entity_field: type - plugin_id: commerce_entity_bundle - operations: - id: operations - table: wishlist_template - field: operations - relationship: none - group_type: group - admin_label: '' - label: Operations - exclude: false - alter: - alter_text: false - text: '' - make_link: false - path: '' - absolute: false - external: false - replace_spaces: false - path_case: none - trim_whitespace: false - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: false - max_length: 0 - word_boundary: true - ellipsis: true - more_link: false - more_link_text: '' - more_link_path: '' - strip_tags: false - trim: false - preserve_tags: '' - html: false - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: true - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: true - empty: '' - hide_empty: false - empty_zero: false - hide_alter_empty: true - destination: false - entity_type: wishlist_template - plugin_id: entity_operations - filters: - type: - id: type - table: wishlist_template_field_data - field: type - relationship: none - group_type: group - admin_label: '' - operator: in - value: { } - group: 1 - exposed: true - expose: - operator_id: type_op - label: Type - description: '' - use_operator: false - operator: type_op - identifier: type - required: false - remember: false - multiple: false - remember_roles: - authenticated: authenticated - anonymous: '0' - administrator: '0' - reduce: false - hide_single_bundle: true - is_grouped: false - group_info: - label: '' - description: '' - identifier: '' - optional: true - widget: select - multiple: false - remember: false - default_group: All - default_group_multiple: { } - group_items: { } - entity_type: wishlist_template - entity_field: type - plugin_id: commerce_entity_bundle - sorts: { } - title: Stores - header: { } - footer: { } - empty: - area_text_custom: - id: area_text_custom - table: views - field: area_text_custom - relationship: none - group_type: group - admin_label: '' - empty: true - tokenize: false - content: 'No wishlist templates available.' - plugin_id: text_custom - relationships: { } - arguments: { } - display_extenders: { } - cache_metadata: - contexts: - - 'languages:language_content' - - 'languages:language_interface' - - url.query_args - - user.permissions - cacheable: false - max-age: 0 - tags: { } - page_1: - display_plugin: page - id: page_1 - display_title: Page - position: 1 - display_options: - display_extenders: { } - path: admin/commerce/wishlist_templates - cache_metadata: - contexts: - - 'languages:language_content' - - 'languages:language_interface' - - url.query_args - - user.permissions - cacheable: false - max-age: 0 - tags: { } diff --git a/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml b/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml deleted file mode 100644 index 7d477e7..0000000 --- a/wishlist_template/config/install/wishlist_template.wishlist_template_type.default.yml +++ /dev/null @@ -1,6 +0,0 @@ -langcode: en -status: true -dependencies: { } -id: default -label: Default -description: null diff --git a/wishlist_template/config/schema/wishlist_template.schema.yml b/wishlist_template/config/schema/wishlist_template.schema.yml deleted file mode 100644 index 6c6a0db..0000000 --- a/wishlist_template/config/schema/wishlist_template.schema.yml +++ /dev/null @@ -1,22 +0,0 @@ -wishlist_template.wishlist_template_type.*: - type: config_entity - label: 'Wishlist Tempalte type config' - mapping: - id: - type: string - label: 'Type' - label: - type: label - label: 'Label' - uuid: - type: string - description: - type: text - label: 'Description' - -views.field.wishlist_template: - type: views.field.field - mapping: - hide_single_wishlist_template: - type: boolean - label: 'Hide if there''s only one wishlist template.' diff --git a/wishlist_template/config/schema/wishlist_template_type.schema.yml b/wishlist_template/config/schema/wishlist_template_type.schema.yml new file mode 100644 index 0000000..7c07e3d --- /dev/null +++ b/wishlist_template/config/schema/wishlist_template_type.schema.yml @@ -0,0 +1,12 @@ +wishlist_template.wishlist_template_type.*: + type: config_entity + label: 'Wishlist template type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php b/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php deleted file mode 100644 index dedde55..0000000 --- a/wishlist_template/src/Entity/EntityWishlistTemplateInterface.php +++ /dev/null @@ -1,46 +0,0 @@ - \Drupal::currentUser()->id(), + ); + } + + /** + * {@inheritdoc} + */ + public function getType() { + return $this->bundle(); + } + /** * {@inheritdoc} */ @@ -82,30 +96,60 @@ public function setName($name) { /** * {@inheritdoc} */ - public function getOwner() { - return $this->get('uid')->entity; + public function getCreatedTime() { + return $this->get('created')->value; } /** * {@inheritdoc} */ - public function setOwner(UserInterface $account) { - $this->set('uid', $account->id()); + public function setCreatedTime($timestamp) { + $this->set('created', $timestamp); return $this; } + /** + * {@inheritdoc} + */ + public function getOwner() { + return $this->get('user_id')->entity; + } + /** * {@inheritdoc} */ public function getOwnerId() { - return $this->get('uid')->target_id; + return $this->get('user_id')->target_id; } /** * {@inheritdoc} */ public function setOwnerId($uid) { - $this->set('uid', $uid); + $this->set('user_id', $uid); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setOwner(UserInterface $account) { + $this->set('user_id', $account->id()); + return $this; + } + + /** + * {@inheritdoc} + */ + public function isPublished() { + return (bool) $this->getEntityKey('status'); + } + + /** + * {@inheritdoc} + */ + public function setPublished($published) { + $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED); return $this; } @@ -145,37 +189,63 @@ public function setTerms($terms) { public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); - $fields['type'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Type')) - ->setDescription(t('The wishlist template type.')) - ->setSetting('target_type', 'wishlist_template_type') - ->setReadOnly(TRUE); - - $fields['uid'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Owner')) - ->setDescription(t('The store owner.')) - ->setDefaultValueCallback('Drupal\commerce_store\Entity\Store::getCurrentUserId') + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Authored by')) + ->setDescription(t('The user ID of author of the Wishlist template entity.')) + ->setRevisionable(TRUE) ->setSetting('target_type', 'user') - ->setDisplayOptions('form', [ + ->setSetting('handler', 'default') + ->setTranslatable(TRUE) + ->setDisplayOptions('view', array( + 'label' => 'hidden', + 'type' => 'author', + 'weight' => 0, + )) + ->setDisplayOptions('form', array( 'type' => 'entity_reference_autocomplete', - 'weight' => 50, - ]); + 'weight' => 5, + 'settings' => array( + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ), + )) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) - ->setDescription(t('The wishlist template name.')) - ->setRequired(TRUE) - ->setTranslatable(TRUE) - ->setSettings([ - 'default_value' => '', - 'max_length' => 255, - ]) - ->setDisplayOptions('form', [ + ->setDescription(t('The name of the Wishlist template entity.')) + ->setSettings(array( + 'max_length' => 50, + 'text_processing' => 0, + )) + ->setDefaultValue('') + ->setDisplayOptions('view', array( + 'label' => 'above', + 'type' => 'string', + 'weight' => -4, + )) + ->setDisplayOptions('form', array( 'type' => 'string_textfield', - 'weight' => 0, - ]) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayConfigurable('form', TRUE); + 'weight' => -4, + )) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['status'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Publishing status')) + ->setDescription(t('A boolean indicating whether the Wishlist template is published.')) + ->setDefaultValue(TRUE); + + $fields['created'] = BaseFieldDefinition::create('created') + ->setLabel(t('Created')) + ->setDescription(t('The time that the entity was created.')); + + $fields['changed'] = BaseFieldDefinition::create('changed') + ->setLabel(t('Changed')) + ->setDescription(t('The time that the entity was last edited.')); $fields['taxonomy_term_view_mode'] = BaseFieldDefinition::create('list_string') ->setLabel(t('Category view mode')) @@ -208,15 +278,4 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { return $fields; } - /** - * Default value callback for 'uid' base field definition. - * - * @see ::baseFieldDefinitions() - * - * @return array - * An array of default values. - */ - public static function getCurrentUserId() { - return [\Drupal::currentUser()->id()]; - } } diff --git a/wishlist_template/src/Entity/WishlistTemplateInterface.php b/wishlist_template/src/Entity/WishlistTemplateInterface.php index b29419d..fcc6842 100644 --- a/wishlist_template/src/Entity/WishlistTemplateInterface.php +++ b/wishlist_template/src/Entity/WishlistTemplateInterface.php @@ -3,31 +3,86 @@ namespace Drupal\wishlist_template\Entity; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityChangedInterface; use Drupal\user\EntityOwnerInterface; /** - * Defines the interface for wishlist_templates. + * Provides an interface for defining Wishlist template entities. + * + * @ingroup wishlist_template */ -interface WishlistTemplateInterface extends ContentEntityInterface, EntityOwnerInterface { +interface WishlistTemplateInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface { + + // Add get/set methods for your configuration properties here. /** - * Gets the wishlist_template name. + * Gets the Wishlist template type. * * @return string - * The wishlist_template name. + * The Wishlist template type. + */ + public function getType(); + + /** + * Gets the Wishlist template name. + * + * @return string + * Name of the Wishlist template. */ public function getName(); /** - * Sets the wishlist_template name. + * Sets the Wishlist template name. * * @param string $name - * The wishlist_template name. + * The Wishlist template name. * - * @return $this + * @return \Drupal\wishlist_template\Entity\WishlistTemplateInterface + * The called Wishlist template entity. */ public function setName($name); + /** + * Gets the Wishlist template creation timestamp. + * + * @return int + * Creation timestamp of the Wishlist template. + */ + public function getCreatedTime(); + + /** + * Sets the Wishlist template creation timestamp. + * + * @param int $timestamp + * The Wishlist template creation timestamp. + * + * @return \Drupal\wishlist_template\Entity\WishlistTemplateInterface + * The called Wishlist template entity. + */ + public function setCreatedTime($timestamp); + + /** + * Returns the Wishlist template published status indicator. + * + * Unpublished Wishlist template are only visible to restricted users. + * + * @return bool + * TRUE if the Wishlist template is published. + */ + public function isPublished(); + + /** + * Sets the published status of a Wishlist template. + * + * @param bool $published + * TRUE to set this Wishlist template to published, FALSE to set it to unpublished. + * + * @return \Drupal\wishlist_template\Entity\WishlistTemplateInterface + * The called Wishlist template entity. + */ + public function setPublished($published); + + /** * Gets the the view mode used for rendering the terms. * @@ -63,5 +118,4 @@ public function getTerms(); * @return $this */ public function setTerms($terms); - } diff --git a/wishlist_template/src/Entity/WishlistTemplateType.php b/wishlist_template/src/Entity/WishlistTemplateType.php index 7c8d919..b44e13d 100644 --- a/wishlist_template/src/Entity/WishlistTemplateType.php +++ b/wishlist_template/src/Entity/WishlistTemplateType.php @@ -5,17 +5,11 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; /** - * Defines the wishlist template type entity class. + * Defines the Wishlist template type entity. * * @ConfigEntityType( * id = "wishlist_template_type", * label = @Translation("Wishlist template type"), - * label_singular = @Translation("Wishlist template type"), - * label_plural = @Translation("Wishlist template types"), - * label_count = @PluralTranslation( - * singular = "@count wishlist template type", - * plural = "@count wishlist template types", - * ), * handlers = { * "list_builder" = "Drupal\wishlist_template\WishlistTemplateTypeListBuilder", * "form" = { @@ -24,67 +18,40 @@ * "delete" = "Drupal\wishlist_template\Form\WishlistTemplateTypeDeleteForm" * }, * "route_provider" = { - * "default" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider", + * "html" = "Drupal\wishlist_template\WishlistTemplateTypeHtmlRouteProvider", * }, * }, - * admin_permission = "administer wishlist template types", * config_prefix = "wishlist_template_type", + * admin_permission = "administer site configuration", * bundle_of = "wishlist_template", * entity_keys = { * "id" = "id", * "label" = "label", - * "uuid" = "uuid", - * }, - * config_export = { - * "id", - * "label", - * "uuid", - * "description", + * "uuid" = "uuid" * }, * links = { - * "add-form" = "/admin/commerce/config/wishlist-template-types/add", - * "edit-form" = "/admin/commerce/config/wishlist-template-types/{wishlist_template_type}/edit", - * "delete-form" = "/admin/commerce/config/wishlist-template-types/{wishlist_template_type}/delete", - * "collection" = "/admin/commerce/config/wishlist-template-types", + * "canonical" = "/admin/commerce/config/wishlist_template_type/{wishlist_template_type}", + * "add-form" = "/admin/commerce/config/wishlist_template_type/add", + * "edit-form" = "/admin/commerce/config/wishlist_template_type/{wishlist_template_type}/edit", + * "delete-form" = "/admin/commerce/config/wishlist_template_type/{wishlist_template_type}/delete", + * "collection" = "/admin/commerce/config/wishlist_template_type" * } * ) */ class WishlistTemplateType extends ConfigEntityBundleBase implements WishlistTemplateTypeInterface { /** - * The wishlist template type ID. + * The Wishlist template type ID. * * @var string */ protected $id; /** - * The wishlist template type label. + * The Wishlist template type label. * * @var string */ protected $label; - /** - * A brief description of this wishlist template type. - * - * @var string - */ - protected $description; - - /** - * {@inheritdoc} - */ - public function getDescription() { - return $this->description; - } - - /** - * {@inheritdoc} - */ - public function setDescription($description) { - $this->description = $description; - return $this; - } - } diff --git a/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php b/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php index b873802..f90515a 100644 --- a/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php +++ b/wishlist_template/src/Entity/WishlistTemplateTypeInterface.php @@ -3,10 +3,11 @@ namespace Drupal\wishlist_template\Entity; use Drupal\Core\Config\Entity\ConfigEntityInterface; -use Drupal\Core\Entity\EntityDescriptionInterface; /** - * Defines the interface for wishlist template types. + * Provides an interface for defining Wishlist template type entities. */ -interface WishlistTemplateTypeInterface extends ConfigEntityInterface, EntityDescriptionInterface { +interface WishlistTemplateTypeInterface extends ConfigEntityInterface { + + // Add get/set methods for your configuration properties here. } diff --git a/wishlist_template/src/Entity/WishlistTemplateViewsData.php b/wishlist_template/src/Entity/WishlistTemplateViewsData.php new file mode 100644 index 0000000..2ebe348 --- /dev/null +++ b/wishlist_template/src/Entity/WishlistTemplateViewsData.php @@ -0,0 +1,28 @@ + 'id', + 'title' => $this->t('Wishlist template'), + 'help' => $this->t('The Wishlist template ID.'), + ); + + return $data; + } + +} diff --git a/wishlist_template/src/Event/WishlistTemplateEvent.php b/wishlist_template/src/Event/WishlistTemplateEvent.php deleted file mode 100644 index 0c7d892..0000000 --- a/wishlist_template/src/Event/WishlistTemplateEvent.php +++ /dev/null @@ -1,42 +0,0 @@ -wishlist_template = $wishlist; - } - - /** - * Gets the wishlist template. - * - * @return \Drupal\wishlist_template\Entity\WishlistTemplateInterface - * The wishlist template. - */ - public function getWishlisttemplate() { - return $this->wishlist_template; - } - -} diff --git a/wishlist_template/src/Event/WishlistTemplateEvents.php b/wishlist_template/src/Event/WishlistTemplateEvents.php deleted file mode 100644 index 37f1076..0000000 --- a/wishlist_template/src/Event/WishlistTemplateEvents.php +++ /dev/null @@ -1,90 +0,0 @@ -entity; + public function buildForm(array $form, FormStateInterface $form_state) { + /* @var $entity \Drupal\wishlist_template\Entity\WishlistTemplate */ + $form = parent::buildForm($form, $form_state); + $entity = $this->entity; return $form; } /** - * Overrides Drupal\Core\Entity\EntityFormController::save(). + * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - $this->entity->save(); - drupal_set_message($this->t('Saved the %label wishlist template.', [ - '%label' => $this->entity->label(), - ])); - $form_state->setRedirect('entity.wishlist_template.collection'); + $entity = $this->entity; + $status = parent::save($form, $form_state); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label Wishlist template.', [ + '%label' => $entity->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Wishlist template.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.wishlist_template.canonical', ['wishlist_template' => $entity->id()]); } } diff --git a/wishlist_template/src/Form/WishlistTemplateSettingsForm.php b/wishlist_template/src/Form/WishlistTemplateSettingsForm.php new file mode 100644 index 0000000..abd4cbc --- /dev/null +++ b/wishlist_template/src/Form/WishlistTemplateSettingsForm.php @@ -0,0 +1,55 @@ +t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); + } /** - * Constructs a new WishlistTemplateTypeDeleteForm object. - * - * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory - * The entity query object. + * {@inheritdoc} */ - public function __construct(QueryFactory $query_factory) { - $this->queryFactory = $query_factory; + public function getCancelUrl() { + return new Url('entity.wishlist_template_type.collection'); } /** * {@inheritdoc} */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.query') - ); + public function getConfirmText() { + return $this->t('Delete'); } /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $wishlist_template_count = $this->queryFactory->get('wishlist_template') - ->condition('type', $this->entity->id()) - ->count() - ->execute(); - if ($wishlist_template_count) { - $caption = '

' . $this->formatPlural($wishlist_template_count, '%type is used by 1 wishlist template on your site. You can not remove this wishlist template type until you have removed all of the %type wishlist templates.', '%type is used by @count wishlist templates on your site. You may not remove %type until you have removed all of the %type wishlist templates.', ['%type' => $this->entity->label()]) . '

'; - $form['#title'] = $this->getQuestion(); - $form['description'] = ['#markup' => $caption]; - return $form; - } - - return parent::buildForm($form, $form_state); + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->entity->delete(); + + drupal_set_message( + $this->t('content @type: deleted @label.', + [ + '@type' => $this->entity->bundle(), + '@label' => $this->entity->label(), + ] + ) + ); + + $form_state->setRedirectUrl($this->getCancelUrl()); } } diff --git a/wishlist_template/src/Form/WishlistTemplateTypeForm.php b/wishlist_template/src/Form/WishlistTemplateTypeForm.php index 118c5d1..1bed548 100644 --- a/wishlist_template/src/Form/WishlistTemplateTypeForm.php +++ b/wishlist_template/src/Form/WishlistTemplateTypeForm.php @@ -2,72 +2,66 @@ namespace Drupal\wishlist_template\Form; -use Drupal\Core\Entity\BundleEntityFormBase; +use Drupal\Core\Entity\EntityForm; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\language\Entity\ContentLanguageSettings; -class WishlistTemplateTypeForm extends BundleEntityFormBase { +/** + * Class WishlistTemplateTypeForm. + * + * @package Drupal\wishlist_template\Form + */ +class WishlistTemplateTypeForm extends EntityForm { /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var $wishlist_template_type \Drupal\wishlist_template\Entity\WishlistTemplateType */ - $wishlist_template_type = $this->entity; + $wishlist_template_type = $this->entity; $form['label'] = [ '#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $wishlist_template_type->label(), - '#description' => $this->t('Label for the wishlist template type.'), + '#description' => $this->t("Label for the Wishlist template type."), '#required' => TRUE, ]; + $form['id'] = [ '#type' => 'machine_name', '#default_value' => $wishlist_template_type->id(), '#machine_name' => [ 'exists' => '\Drupal\wishlist_template\Entity\WishlistTemplateType::load', ], - '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, - ]; - $form['description'] = [ - '#type' => 'textfield', - '#title' => $this->t('Description'), - '#default_value' => $wishlist_template_type->getDescription(), + '#disabled' => !$wishlist_template_type->isNew(), ]; - if ($this->moduleHandler->moduleExists('language')) { - $form['language'] = [ - '#type' => 'details', - '#title' => $this->t('Language settings'), - '#group' => 'additional_settings', - ]; - $form['language']['language_configuration'] = [ - '#type' => 'language_configuration', - '#entity_information' => [ - 'entity_type' => 'wishlist_template', - 'bundle' => $wishlist_template_type->id(), - ], - '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('wishlist_template', $wishlist_template_type->id()), - ]; - $form['#submit'][] = 'language_configuration_element_submit'; - } + /* You will need additional form elements for your custom properties. */ - return $this->protectBundleIdElement($form); + return $form; } /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - $this->entity->save(); - drupal_set_message($this->t('Saved the %label wishlist template type.', [ - '%label' => $this->entity->label(), - ])); - $form_state->setRedirect('entity.wishlist_template_type.collection'); + $wishlist_template_type = $this->entity; + $status = $wishlist_template_type->save(); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label Wishlist template type.', [ + '%label' => $wishlist_template_type->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Wishlist template type.', [ + '%label' => $wishlist_template_type->label(), + ])); + } + $form_state->setRedirectUrl($wishlist_template_type->urlInfo('collection')); } } diff --git a/wishlist_template/src/Plugin/views/field/WishlistTemplate.php b/wishlist_template/src/Plugin/views/field/WishlistTemplate.php deleted file mode 100644 index c79927b..0000000 --- a/wishlist_template/src/Plugin/views/field/WishlistTemplate.php +++ /dev/null @@ -1,56 +0,0 @@ - TRUE]; - - return $options; - } - - /** - * {@inheritdoc} - */ - public function buildOptionsForm(&$form, FormStateInterface $form_state) { - parent::buildOptionsForm($form, $form_state); - - $form['hide_single_wishlist_template'] = [ - '#type' => 'checkbox', - '#title' => t("Hide if there's only one wishlist template."), - '#default_value' => $this->options['hide_single_wishlist_template'], - ]; - } - - /** - * {@inheritdoc} - */ - public function access(AccountInterface $account) { - $wishlist_template_query = $this->entityManager->getStorage('wishlist_template')->getQuery(); - $wishlist_template_count = $wishlist_template_query->count()->execute(); - if ($this->options['hide_single_wishlist_template'] && $wishlist_template_count <= 1) { - return FALSE; - } - - return parent::access($account); - } - -} diff --git a/wishlist_template/src/WishlistTemplateAccessControlHandler.php b/wishlist_template/src/WishlistTemplateAccessControlHandler.php new file mode 100644 index 0000000..54c1e97 --- /dev/null +++ b/wishlist_template/src/WishlistTemplateAccessControlHandler.php @@ -0,0 +1,47 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished wishlist template entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published wishlist template entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit wishlist template entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete wishlist template entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add wishlist template entities'); + } + +} diff --git a/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php b/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php new file mode 100644 index 0000000..68d47dc --- /dev/null +++ b/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php @@ -0,0 +1,85 @@ +id(); + + if ($collection_route = $this->getCollectionRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.collection", $collection_route); + } + + if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) { + $collection->add("$entity_type_id.settings", $settings_form_route); + } + + return $collection; + } + + /** + * Gets the collection route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getCollectionRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { + $entity_type_id = $entity_type->id(); + $route = new Route($entity_type->getLinkTemplate('collection')); + $route + ->setDefaults([ + '_entity_list' => $entity_type_id, + '_title' => "{$entity_type->getLabel()} list", + ]) + ->setRequirement('_permission', 'access wishlist template overview') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * Gets the settings form route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getSettingsFormRoute(EntityTypeInterface $entity_type) { + if (!$entity_type->getBundleEntityType()) { + $route = new Route("/admin/structure/{$entity_type->id()}/settings"); + $route + ->setDefaults([ + '_form' => 'Drupal\wishlist_template\Form\WishlistTemplateSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/wishlist_template/src/WishlistTemplateListBuilder.php b/wishlist_template/src/WishlistTemplateListBuilder.php index 3d9b2ea..0d99ae6 100644 --- a/wishlist_template/src/WishlistTemplateListBuilder.php +++ b/wishlist_template/src/WishlistTemplateListBuilder.php @@ -2,21 +2,26 @@ namespace Drupal\wishlist_template; -use Drupal\wishlist_template\Entity\WishlistTemplateType; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityListBuilder; +use Drupal\Core\Routing\LinkGeneratorTrait; +use Drupal\Core\Url; /** - * Defines the list builder for wishlist_templates. + * Defines a class to build a listing of Wishlist template entities. + * + * @ingroup wishlist_template */ class WishlistTemplateListBuilder extends EntityListBuilder { + use LinkGeneratorTrait; + /** * {@inheritdoc} */ public function buildHeader() { - $header['name'] = t('Name'); - $header['type'] = t('Type'); + $header['id'] = $this->t('Wishlist template ID'); + $header['name'] = $this->t('Name'); return $header + parent::buildHeader(); } @@ -24,15 +29,16 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - /** @var \Drupal\wishlist_template\Entity\WishlistTemplateInterface $entity */ - $wishlist_template_type = WishlistTemplateType::load($entity->bundle()); - - $row['name']['data'] = [ - '#type' => 'link', - '#title' => $entity->label(), - ] + $entity->toUrl()->toRenderArray(); - $row['type'] = $wishlist_template_type->label(); - + /* @var $entity \Drupal\wishlist_template\Entity\WishlistTemplate */ + $row['id'] = $entity->id(); + $row['name'] = $this->l( + $entity->label(), + new Url( + 'entity.wishlist_template.edit_form', array( + 'wishlist_template' => $entity->id(), + ) + ) + ); return $row + parent::buildRow($entity); } diff --git a/wishlist_template/src/WishlistTemplateStorage.php b/wishlist_template/src/WishlistTemplateStorage.php deleted file mode 100644 index a1342c4..0000000 --- a/wishlist_template/src/WishlistTemplateStorage.php +++ /dev/null @@ -1,66 +0,0 @@ -configFactory = $config_factory; - } - - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) { - return new static( - $entityType, - $container->get('database'), - $container->get('entity.manager'), - $container->get('cache.entity'), - $container->get('language_manager'), - $container->get('event_dispatcher'), - $container->get('config.factory') - ); - } - -} diff --git a/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php b/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php new file mode 100644 index 0000000..b703e1c --- /dev/null +++ b/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php @@ -0,0 +1,59 @@ +id(); + + if ($collection_route = $this->getCollectionRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.collection", $collection_route); + } + + return $collection; + } + + /** + * Gets the collection route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getCollectionRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { + $entity_type_id = $entity_type->id(); + $route = new Route($entity_type->getLinkTemplate('collection')); + $route + ->setDefaults([ + '_entity_list' => $entity_type_id, + // Make sure this is not a TranslatableMarkup object as the + // TitleResolver translates this string again. + '_title' => (string) $entity_type->getLabel(), + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/wishlist_template/src/WishlistTemplateTypeListBuilder.php b/wishlist_template/src/WishlistTemplateTypeListBuilder.php index 10d5330..53a3e27 100644 --- a/wishlist_template/src/WishlistTemplateTypeListBuilder.php +++ b/wishlist_template/src/WishlistTemplateTypeListBuilder.php @@ -6,7 +6,7 @@ use Drupal\Core\Entity\EntityInterface; /** - * Defines the list builder for wishlist_template types. + * Provides a listing of Wishlist template type entities. */ class WishlistTemplateTypeListBuilder extends ConfigEntityListBuilder { @@ -14,7 +14,7 @@ class WishlistTemplateTypeListBuilder extends ConfigEntityListBuilder { * {@inheritdoc} */ public function buildHeader() { - $header['label'] = $this->t('Wishlist Template type'); + $header['label'] = $this->t('Wishlist template type'); $header['id'] = $this->t('Machine name'); return $header + parent::buildHeader(); } @@ -25,6 +25,7 @@ public function buildHeader() { public function buildRow(EntityInterface $entity) { $row['label'] = $entity->label(); $row['id'] = $entity->id(); + // You probably want a few more properties here... return $row + parent::buildRow($entity); } diff --git a/wishlist_template/templates/wishlist-template-content-add-list.html.twig b/wishlist_template/templates/wishlist-template-content-add-list.html.twig new file mode 100644 index 0000000..ede5e34 --- /dev/null +++ b/wishlist_template/templates/wishlist-template-content-add-list.html.twig @@ -0,0 +1,23 @@ +{# +/** + * @file + * Default theme implementation to present a list of custom content entity types/bundles. + * + * Available variables: + * - types: A collection of all the available custom entity types/bundles. + * Each type/bundle contains the following: + * - link: A link to add a content entity of this type. + * - description: A description of this content entity types/bundle. + * + * @see template_preprocess_wishlist_template_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/wishlist_template/templates/wishlist-template.html.twig b/wishlist_template/templates/wishlist-template.html.twig deleted file mode 100644 index 303c8f0..0000000 --- a/wishlist_template/templates/wishlist-template.html.twig +++ /dev/null @@ -1,24 +0,0 @@ -{# -/** - * @file - * - * Default template for wishlist templates. - * - * Available variables: - * - attributes: HTML attributes for the wrapper. - * - wishlist_template: The rendered wishlist template fields. - * Use 'wishlist_template' to print them all, or print a subset such as - * 'wishlist_template.name'. Use the following code to exclude the - * printing of a given field: - * @code - * {{ wishlist_template|without('name') }} - * @endcode - * - wishlist_template_entity: The wishlist template entity. - * - wishlist_template_url: The wishlist template URL. - * - * @ingroup themeable - */ -#} - - {{ wishlist_template }} - diff --git a/wishlist_template/templates/wishlist_template.html.twig b/wishlist_template/templates/wishlist_template.html.twig new file mode 100644 index 0000000..84a2d5c --- /dev/null +++ b/wishlist_template/templates/wishlist_template.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file wishlist_template.html.twig + * Default theme implementation to present Wishlist template data. + * + * This template is used when viewing Wishlist template pages. + * + * + * Available variables: + * - content: A list of content items. Use 'content' to print all content, or + * - attributes: HTML attributes for the container element. + * + * @see template_preprocess_wishlist_template() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/wishlist_template/wishlist_template.info.yml b/wishlist_template/wishlist_template.info.yml index 6bc2354..d438803 100644 --- a/wishlist_template/wishlist_template.info.yml +++ b/wishlist_template/wishlist_template.info.yml @@ -7,9 +7,4 @@ dependencies: - commerce_wishlist - entity_reference - options -config_devel: - install: - - wishlist_template.wishlist_template_type.default - - core.entity_view_display.wishlist_template.default.default - - views.view.wishlist_template - - system.action.wishlist_template_delete_action + - entity diff --git a/wishlist_template/wishlist_template.links.action.yml b/wishlist_template/wishlist_template.links.action.yml index d3f1d78..ec144d0 100644 --- a/wishlist_template/wishlist_template.links.action.yml +++ b/wishlist_template/wishlist_template.links.action.yml @@ -1,11 +1,11 @@ -entity.wishlist_template.add_page: - route_name: entity.wishlist_template.add_page - title: 'Add wishlist template' +entity.wishlist_template.add_form: + route_name: 'entity.wishlist_template.add_page' + title: 'Add Wishlist template' appears_on: - entity.wishlist_template.collection - entity.wishlist_template_type.add_form: - route_name: entity.wishlist_template_type.add_form - title: 'Add wishlist template type' + route_name: 'entity.wishlist_template_type.add_form' + title: 'Add Wishlist template type' appears_on: - - entity.wishlist_template_type.collection \ No newline at end of file + - entity.wishlist_template_type.collection + diff --git a/wishlist_template/wishlist_template.links.menu.yml b/wishlist_template/wishlist_template.links.menu.yml index bccb440..e19578e 100644 --- a/wishlist_template/wishlist_template.links.menu.yml +++ b/wishlist_template/wishlist_template.links.menu.yml @@ -1,11 +1,16 @@ +# Wishlist template menu items definition entity.wishlist_template.collection: - title: 'Wishlist template' - route_name: 'entity.wishlist_template.collection' + title: 'Wishlist template list' + route_name: entity.wishlist_template.collection + description: 'List Wishlist template entities' parent: 'commerce.admin_commerce' - description: 'Manage your wishlist templates.' + weight: 100 +# Wishlist template type menu items definition entity.wishlist_template_type.collection: - title: 'Wishlist template types' - route_name: 'entity.wishlist_template_type.collection' + title: 'Wishlist template type' + route_name: entity.wishlist_template_type.collection + description: 'List Wishlist template type (bundles)' parent: 'commerce.configuration' - description: 'Manage your wishlist template types.' + weight: 99 + diff --git a/wishlist_template/wishlist_template.links.task.yml b/wishlist_template/wishlist_template.links.task.yml index 95d7a57..44cfc09 100644 --- a/wishlist_template/wishlist_template.links.task.yml +++ b/wishlist_template/wishlist_template.links.task.yml @@ -1,24 +1,16 @@ +# Wishlist template routing definition entity.wishlist_template.canonical: route_name: entity.wishlist_template.canonical base_route: entity.wishlist_template.canonical - title: View - -entity.wishlist_template.collection: - route_name: entity.wishlist_template.collection - base_route: entity.wishlist_template.collection - title: List + title: 'View' entity.wishlist_template.edit_form: route_name: entity.wishlist_template.edit_form - base_route: entity.wishlist_template.edit_form + base_route: entity.wishlist_template.canonical title: Edit -entity.wishlist_template_type.collection: - route_name: entity.wishlist_template_type.collection - route_name: entity.wishlist_template_type.collection - title: List - -entity.wishlist_template_type.edit_form: - route_name: entity.wishlist_template_type.edit_form - base_route: entity.wishlist_template_type.edit_form - title: Edit +entity.wishlist_template.delete_form: + route_name: entity.wishlist_template.delete_form + base_route: entity.wishlist_template.canonical + title: Delete + weight: 10 diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module index 7f1b792..ca6756f 100644 --- a/wishlist_template/wishlist_template.module +++ b/wishlist_template/wishlist_template.module @@ -2,69 +2,79 @@ /** * @file - * Defines the Wishlist template entity and associated features. + * Contains wishlist_template.module.. */ -use Drupal\Core\Render\Element; +use Drupal\Core\Routing\RouteMatchInterface; /** - * Callback function that returns a list of taxonomy view modes. + * Implements hook_help(). */ -function _wishlist_template_term_view_mode_values() { - // Grab the View Mode service. - $entity_display_repository = \Drupal::service('entity_display.repository'); +function wishlist_template_help($route_name, RouteMatchInterface $route_match) { + switch ($route_name) { + // Main module help for the wishlist_template module. + case 'help.page.wishlist_template': + $output = ''; + $output .= '

' . t('About') . '

'; + $output .= '

' . t('Description') . '

'; + return $output; - // The entity type for which we are looking. - $entity_type_id = "taxonomy_term"; - - // Use the service to get an array of available modes. - $view_modes = $entity_display_repository->getViewModes($entity_type_id); - - // Create a simple "allowed_values" array - $allowed_values = array(); - foreach ($view_modes as $id => $view_mode) { - $allowed_values[$id] = $view_mode['label'] . ' (' . $id . ')'; + default: } - - return $allowed_values; } /** * Implements hook_theme(). */ function wishlist_template_theme() { - return [ - 'wishlist_template' => [ - 'render element' => 'elements', - ], + $theme = []; + $theme['wishlist_template'] = array( + 'render element' => 'elements', + 'file' => 'wishlist_template.page.inc', + 'template' => 'wishlist_template', + ); + $theme['wishlist_template_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'wishlist_template.page.inc', ]; + return $theme; } /** - * Implements hook_theme_suggestions_wishlist_template(). - */ +* Implements hook_theme_suggestions_HOOK(). +*/ function wishlist_template_theme_suggestions_wishlist_template(array $variables) { - return _commerce_entity_theme_suggestions('wishlist_template', $variables); + $suggestions = array(); + $entity = $variables['elements']['#wishlist_template']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'wishlist_template__' . $sanitized_view_mode; + $suggestions[] = 'wishlist_template__' . $entity->bundle(); + $suggestions[] = 'wishlist_template__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'wishlist_template__' . $entity->id(); + $suggestions[] = 'wishlist_template__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; } /** - * Prepares variables for wishlist template templates. - * - * Default template: wishlist-template.html.twig. - * - * @param array $variables - * An associative array containing: - * - elements: An associative array containing rendered fields. - * - attributes: HTML attributes for the containing element. + * Callback function that returns a list of taxonomy view modes. */ -function template_preprocess_wishlist_template(array &$variables) { - /** @var Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template */ - $wishlist_template = $variables['elements']['#wishlist_template']; +function _wishlist_template_term_view_mode_values() { + // Grab the View Mode service. + $entity_display_repository = \Drupal::service('entity_display.repository'); - $variables['wishlist_template_entity'] = $wishlist_template; - $variables['wishlist_template_url'] = $wishlist_template->toUrl(); - $variables['wishlist_template'] = []; - foreach (Element::children($variables['elements']) as $key) { - $variables['wishlist_template'][$key] = $variables['elements'][$key]; + // The entity type for which we are looking. + $entity_type_id = "taxonomy_term"; + + // Use the service to get an array of available modes. + $view_modes = $entity_display_repository->getViewModes($entity_type_id); + + // Create a simple "allowed_values" array + $allowed_values = array(); + foreach ($view_modes as $id => $view_mode) { + $allowed_values[$id] = $view_mode['label'] . ' (' . $id . ')'; } + + return $allowed_values; } diff --git a/wishlist_template/wishlist_template.page.inc b/wishlist_template/wishlist_template.page.inc new file mode 100644 index 0000000..85c52fd --- /dev/null +++ b/wishlist_template/wishlist_template.page.inc @@ -0,0 +1,32 @@ + Date: Wed, 7 Sep 2016 09:14:09 -0400 Subject: [PATCH 05/12] WIP Issue #2794531: Wishlist Templates - It works --- wishlist_template/src/Entity/WishlistTemplate.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/wishlist_template/src/Entity/WishlistTemplate.php b/wishlist_template/src/Entity/WishlistTemplate.php index 740bb70..533ea40 100644 --- a/wishlist_template/src/Entity/WishlistTemplate.php +++ b/wishlist_template/src/Entity/WishlistTemplate.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\user\UserInterface; +use Drupal\Core\Field\FieldStorageDefinitionInterface; /** * Defines the Wishlist template entity. @@ -216,7 +217,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) - ->setDescription(t('The name of the Wishlist template entity.')) + ->setRequired(TRUE) + ->setDescription(t('The name of the Wishlist template.')) ->setSettings(array( 'max_length' => 50, 'text_processing' => 0, @@ -258,7 +260,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ]) ->setDisplayOptions('form', [ 'type' => 'select', - 'weight' => 0, + 'weight' => -3, ]) ->setDisplayOptions('view', [ 'type' => 'hidden', @@ -268,11 +270,18 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['terms'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Categories')) + ->setRequired(TRUE) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE) ->setDescription(t('The categories used for grouping products the creating the wishlist template.')) ->setSetting('target_type', 'taxonomy_term') + ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) ->setDisplayOptions('form', [ 'type' => 'entity_reference_autocomplete', - 'weight' => 50, + 'weight' => -2, + ]) + ->setDisplayOptions('view', [ + 'type' => 'hidden', ]); return $fields; From 5384898f504e27b601af44008702911c21baebb1 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Wed, 7 Sep 2016 12:31:27 -0400 Subject: [PATCH 06/12] WIP Issue #2794531: Wishlist Templates - First blush with a field widget override --- .../Field/FieldWidget/TaxonomyProduct.php | 85 +++++++++++++++++++ .../templates/wishlist-template.html.twig | 24 ++++++ .../templates/wishlist_template.html.twig | 22 ----- 3 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php create mode 100644 wishlist_template/templates/wishlist-template.html.twig delete mode 100644 wishlist_template/templates/wishlist_template.html.twig diff --git a/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php b/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php new file mode 100644 index 0000000..c4fba2b --- /dev/null +++ b/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php @@ -0,0 +1,85 @@ + 60, + 'placeholder' => '', + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $elements = []; + + $elements['size'] = [ + '#type' => 'number', + '#title' => t('Size of textfield'), + '#default_value' => $this->getSetting('size'), + '#required' => TRUE, + '#min' => 1, + ]; + $elements['placeholder'] = [ + '#type' => 'textfield', + '#title' => t('Placeholder'), + '#default_value' => $this->getSetting('placeholder'), + '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), + ]; + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + + $summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]); + if (!empty($this->getSetting('placeholder'))) { + $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $this->getSetting('placeholder')]); + } + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $element['value'] = $element + [ + '#type' => 'textfield', + '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, + '#size' => $this->getSetting('size'), + '#placeholder' => $this->getSetting('placeholder'), + '#maxlength' => $this->getFieldSetting('max_length'), + ]; + + return $element; + } + +} diff --git a/wishlist_template/templates/wishlist-template.html.twig b/wishlist_template/templates/wishlist-template.html.twig new file mode 100644 index 0000000..303c8f0 --- /dev/null +++ b/wishlist_template/templates/wishlist-template.html.twig @@ -0,0 +1,24 @@ +{# +/** + * @file + * + * Default template for wishlist templates. + * + * Available variables: + * - attributes: HTML attributes for the wrapper. + * - wishlist_template: The rendered wishlist template fields. + * Use 'wishlist_template' to print them all, or print a subset such as + * 'wishlist_template.name'. Use the following code to exclude the + * printing of a given field: + * @code + * {{ wishlist_template|without('name') }} + * @endcode + * - wishlist_template_entity: The wishlist template entity. + * - wishlist_template_url: The wishlist template URL. + * + * @ingroup themeable + */ +#} + + {{ wishlist_template }} + diff --git a/wishlist_template/templates/wishlist_template.html.twig b/wishlist_template/templates/wishlist_template.html.twig deleted file mode 100644 index 84a2d5c..0000000 --- a/wishlist_template/templates/wishlist_template.html.twig +++ /dev/null @@ -1,22 +0,0 @@ -{# -/** - * @file wishlist_template.html.twig - * Default theme implementation to present Wishlist template data. - * - * This template is used when viewing Wishlist template pages. - * - * - * Available variables: - * - content: A list of content items. Use 'content' to print all content, or - * - attributes: HTML attributes for the container element. - * - * @see template_preprocess_wishlist_template() - * - * @ingroup themeable - */ -#} - - {% if content %} - {{- content -}} - {% endif %} - From 3ceecda1093ba881c428ab0fca5a71ca8b2ed597 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Wed, 7 Sep 2016 15:44:35 -0400 Subject: [PATCH 07/12] WIP Issue #2794531: Wishlist Templates - Better idea: just ask for products --- .../src/Entity/WishlistTemplate.php | 15 ++++ .../src/Entity/WishlistTemplateInterface.php | 18 ++++ .../Field/FieldWidget/TaxonomyProduct.php | 85 ------------------- 3 files changed, 33 insertions(+), 85 deletions(-) delete mode 100644 wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php diff --git a/wishlist_template/src/Entity/WishlistTemplate.php b/wishlist_template/src/Entity/WishlistTemplate.php index 533ea40..707309c 100644 --- a/wishlist_template/src/Entity/WishlistTemplate.php +++ b/wishlist_template/src/Entity/WishlistTemplate.php @@ -184,6 +184,21 @@ public function setTerms($terms) { return $this; } + /** + * {@inheritdoc} + */ + public function getDefaultProducts() { + return $this->get('default_products')->value; + } + + /** + * {@inheritdoc} + */ + public function setDefaultProducts($terms) { + $this->set('default_products', $terms); + return $this; + } + /** * {@inheritdoc} */ diff --git a/wishlist_template/src/Entity/WishlistTemplateInterface.php b/wishlist_template/src/Entity/WishlistTemplateInterface.php index fcc6842..9d320ff 100644 --- a/wishlist_template/src/Entity/WishlistTemplateInterface.php +++ b/wishlist_template/src/Entity/WishlistTemplateInterface.php @@ -118,4 +118,22 @@ public function getTerms(); * @return $this */ public function setTerms($terms); + + /** + * Gets the default products for the template. + * + * @return array + * The categories for the template. + */ + public function getDefaultProducts(); + + /** + * Sets the default products for the template. + * + * @param array $terms + * The categories for the template. + * + * @return $this + */ + public function setDefaultProducts($terms); } diff --git a/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php b/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php deleted file mode 100644 index c4fba2b..0000000 --- a/wishlist_template/src/Plugin/Field/FieldWidget/TaxonomyProduct.php +++ /dev/null @@ -1,85 +0,0 @@ - 60, - 'placeholder' => '', - ] + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $elements = []; - - $elements['size'] = [ - '#type' => 'number', - '#title' => t('Size of textfield'), - '#default_value' => $this->getSetting('size'), - '#required' => TRUE, - '#min' => 1, - ]; - $elements['placeholder'] = [ - '#type' => 'textfield', - '#title' => t('Placeholder'), - '#default_value' => $this->getSetting('placeholder'), - '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), - ]; - - return $elements; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = []; - - $summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]); - if (!empty($this->getSetting('placeholder'))) { - $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $this->getSetting('placeholder')]); - } - - return $summary; - } - - /** - * {@inheritdoc} - */ - public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $element['value'] = $element + [ - '#type' => 'textfield', - '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, - '#size' => $this->getSetting('size'), - '#placeholder' => $this->getSetting('placeholder'), - '#maxlength' => $this->getFieldSetting('max_length'), - ]; - - return $element; - } - -} From b98f818dcccf7315d3f0d82667c92aba5cbcbdcf Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Fri, 9 Sep 2016 09:31:43 -0400 Subject: [PATCH 08/12] WIP Issue #2794531: Wishlist Templates - tracking views and products --- .../src/Entity/WishlistTemplate.php | 62 +++++++++++++++++ .../src/Entity/WishlistTemplateInterface.php | 19 +++++- .../templates/wishlist-template.html.twig | 24 ------- .../templates/wishlist_template.html.twig | 22 +++++++ wishlist_template/wishlist_template.module | 66 +++++++++++++++++++ 5 files changed, 168 insertions(+), 25 deletions(-) delete mode 100644 wishlist_template/templates/wishlist-template.html.twig create mode 100644 wishlist_template/templates/wishlist_template.html.twig diff --git a/wishlist_template/src/Entity/WishlistTemplate.php b/wishlist_template/src/Entity/WishlistTemplate.php index 707309c..d3aa3ee 100644 --- a/wishlist_template/src/Entity/WishlistTemplate.php +++ b/wishlist_template/src/Entity/WishlistTemplate.php @@ -169,6 +169,21 @@ public function setTaxonomyTermViewMode($taxonomy_term_view_mode) { return $this; } + /** + * {@inheritdoc} + */ + public function setProductListRenderer($product_list_renderer) { + $this->set('product_list_renderer', $product_list_renderer); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getProductListRenderer() { + return $this->get('product_list_renderer')->value; + } + /** * {@inheritdoc} */ @@ -283,6 +298,32 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDisplayConfigurable('view', TRUE) ->setDisplayConfigurable('form', TRUE); + $fields['product_list_renderer'] = BaseFieldDefinition::create('list_string') + ->setLabel(t('View to be used when rendering products.')) + ->setDescription(t('On each template, we will render a taxonomy term and a + list of products that match the term. You can use the + supplied view or clone it and create your own to + quickly enable you to add a product image or sales + price formatter, or whatever this template might need. +

To be included in the above list, views are + required to be a base table of line items and have an + argument for order_id and purchaseable_entity.')) + ->setRequired(TRUE) + ->setTranslatable(TRUE) + ->setSettings([ + 'allowed_values_function' => '_wishlist_template_line_item_views_values', + 'multiple' => false, + ]) + ->setDisplayOptions('form', [ + 'type' => 'select', + 'weight' => -3, + ]) + ->setDisplayOptions('view', [ + 'type' => 'hidden', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + $fields['terms'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Categories')) ->setRequired(TRUE) @@ -299,6 +340,27 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { 'type' => 'hidden', ]); + $fields['default_products'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Default Products')) + ->setRequired(TRUE) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE) + ->setDescription(t('The product(s) that will be added to the wishlist + template on the wishlist creation. Note that we are + referencing the products, not the variations. The default + variation of this product will be added to the + wishlist. This does not currently work with other + types of product architectures.')) + ->setSetting('target_type', 'commerce_product') + ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => -2, + ]) + ->setDisplayOptions('view', [ + 'type' => 'hidden', + ]); + return $fields; } diff --git a/wishlist_template/src/Entity/WishlistTemplateInterface.php b/wishlist_template/src/Entity/WishlistTemplateInterface.php index 9d320ff..1ee7c46 100644 --- a/wishlist_template/src/Entity/WishlistTemplateInterface.php +++ b/wishlist_template/src/Entity/WishlistTemplateInterface.php @@ -82,7 +82,6 @@ public function isPublished(); */ public function setPublished($published); - /** * Gets the the view mode used for rendering the terms. * @@ -91,6 +90,24 @@ public function setPublished($published); */ public function getTaxonomyTermViewMode(); + /** + * Sets the view used to render product variations. + * + * @param string $taxonomy_term_view_mode + * The wishlist_template taxonomy_term_view_mode. + * + * @return $this + */ + public function setProductListRenderer($product_list_renderer); + + /** + * Gets the view used to render product variations. + * + * @return string + * The wishlist_template $product_list_renderer. + */ + public function getProductListRenderer(); + /** * Sets the wishlist_template taxonomy_term_view_mode. * diff --git a/wishlist_template/templates/wishlist-template.html.twig b/wishlist_template/templates/wishlist-template.html.twig deleted file mode 100644 index 303c8f0..0000000 --- a/wishlist_template/templates/wishlist-template.html.twig +++ /dev/null @@ -1,24 +0,0 @@ -{# -/** - * @file - * - * Default template for wishlist templates. - * - * Available variables: - * - attributes: HTML attributes for the wrapper. - * - wishlist_template: The rendered wishlist template fields. - * Use 'wishlist_template' to print them all, or print a subset such as - * 'wishlist_template.name'. Use the following code to exclude the - * printing of a given field: - * @code - * {{ wishlist_template|without('name') }} - * @endcode - * - wishlist_template_entity: The wishlist template entity. - * - wishlist_template_url: The wishlist template URL. - * - * @ingroup themeable - */ -#} - - {{ wishlist_template }} - diff --git a/wishlist_template/templates/wishlist_template.html.twig b/wishlist_template/templates/wishlist_template.html.twig new file mode 100644 index 0000000..84a2d5c --- /dev/null +++ b/wishlist_template/templates/wishlist_template.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file wishlist_template.html.twig + * Default theme implementation to present Wishlist template data. + * + * This template is used when viewing Wishlist template pages. + * + * + * Available variables: + * - content: A list of content items. Use 'content' to print all content, or + * - attributes: HTML attributes for the container element. + * + * @see template_preprocess_wishlist_template() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module index ca6756f..65524cb 100644 --- a/wishlist_template/wishlist_template.module +++ b/wishlist_template/wishlist_template.module @@ -78,3 +78,69 @@ function _wishlist_template_term_view_mode_values() { return $allowed_values; } + +/** + * Create and return a list of views that are capable of rendering variations. + * + * Wishlist template sends the valid order id and a valid list of product ids as + * arguments (in that order) to the selected views from within the entity. + * + * - The base table MUST be 'commerce_line_item'. + * - The first argument MUST be 'order_id'. + * - This will be determined based on the url arguments. + * - The second argument MUST be 'purchased_entity' + * - Make sure you select 'allow multiple' otherwise this won't work as expected. + * - Code and two queries determine all product ids that should show up. + * - We are using Views purely as a very customizable list generator. + * - We have to do it this way to support any number of taxonomy fields and + * any kind of term (regardless of vocabulary). + * + * @return array + */ +function _wishlist_template_line_item_views_values() { + /** @var Drupal\Core\Entity\Query\QueryFactory $query_factory */ + /** @var Drupal\Core\Entity\Query\QueryFactory $query_factory */ + /** @var \Drupal\Core\Entity\Query\QueryInterface $views_query_interface */ + $query_factory = \Drupal::service('entity.query'); + $entity_manager = \Drupal::service('entity.manager'); + $views_query_interface = $query_factory->get('view'); + + // Grab all the line item views. + $views = $views_query_interface->condition('base_table','commerce_line_item')->execute(); + + // "Explode" based on display versions. + $wishlist_views = array(); + $views_default_args = array(); + foreach ($views as $view_id) { + /** @var \Drupal\views\Entity\View $view */ + $view = $entity_manager->getStorage('view')->load($view_id); + $view_displays = array_keys($view->get('display')); + foreach ($view_displays as $view_display) { + $display = $view->getDisplay($view_display); + $argument_keys = array_keys($display['display_options']['arguments']); + + // Track default arguments. See next step for why. + if ($view_display == "default") { + $views_default_args[$view_id] = $argument_keys; + } + + // Inherit default args if no argument keys are found. + if (empty($argument_keys) && !empty($views_default_args[$view_id])) { + $argument_keys = $views_default_args[$view_id]; + } + + // If this view has the appropriate args, return the info. + if (in_array("order_id", $argument_keys) && + in_array("purchased_entity", $argument_keys)) { + $combo_id = $view_id . '---' . $view_display; + $label = $view->label() . ': ' . $view_display; + $pseudo_machine_name = ' (' . $view_id . '.' . $view_display . ')'; + + // This view and display have the valid requirements to render product + // variations. + $wishlist_views[$combo_id] = $label . $pseudo_machine_name; + } + } + } + return $wishlist_views; +} \ No newline at end of file From 9c0e9c05a4ab9a9eb7768db8476aef11c43c786f Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Tue, 13 Sep 2016 10:37:09 -0400 Subject: [PATCH 09/12] WIP Issue #2794531: Wishlist Templates - Viewing template entity --- wishlist_template/wishlist_template.module | 125 ++++++++++++++++++++- 1 file changed, 122 insertions(+), 3 deletions(-) diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module index 65524cb..6e134a9 100644 --- a/wishlist_template/wishlist_template.module +++ b/wishlist_template/wishlist_template.module @@ -98,8 +98,7 @@ function _wishlist_template_term_view_mode_values() { * @return array */ function _wishlist_template_line_item_views_values() { - /** @var Drupal\Core\Entity\Query\QueryFactory $query_factory */ - /** @var Drupal\Core\Entity\Query\QueryFactory $query_factory */ + /** @var \Drupal\Core\Entity\Query\QueryFactory $query_factory */ /** @var \Drupal\Core\Entity\Query\QueryInterface $views_query_interface */ $query_factory = \Drupal::service('entity.query'); $entity_manager = \Drupal::service('entity.manager'); @@ -143,4 +142,124 @@ function _wishlist_template_line_item_views_values() { } } return $wishlist_views; -} \ No newline at end of file +} + +/** + * Implements hook_ENTITY_TYPE_view_alter(). + * + * @param array $build + * @param \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template + * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display + */ +function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) { + $term_view_mode = $wishlist_template->getTaxonomyTermViewMode(); + $product_list_renderer = $wishlist_template->getProductListRenderer(); + $product_list_renderer = explode("---",$product_list_renderer); + $product_list_view = $product_list_renderer[0]; + $product_list_display = $product_list_renderer[1]; + $view_builder = \Drupal::entityManager()->getViewBuilder('taxonomy_term'); + /** + * @var \Drupal\commerce_wishlist\WishlistProviderInterface $wishlist_provider + * @var \Drupal\Core\Session\AccountProxy $current_account + * @var \Drupal\Core\Session\AccountProxy $current_account + **/ + $wishlist_provider = \Drupal::service('commerce_wishlist.wishlist_provider'); + $current_account = \Drupal::service('current_user'); + $wishlists = $wishlist_provider->getWishlists($current_account); + + /** @var \Drupal\commerce_order\Entity\OrderInterface $wishlist */ + $wishlist = false; + if ($wishlists) { + // Look for wishlist template fields + foreach ($wishlists as $wishlist) { + + $fields = array_keys($wishlist->toArray()); + if (in_array('field_test', $fields)) { + // Determine what template is assigned. + // @todo remove hardcoded Order entity requirement. + $values = $wishlist->get("field_test")->getValue(); + if ($values[0]['target_id'] == $wishlist_template->id()) { + // This order has the same value as the template we're viewing. + // Note that $wishlist will remain the order we want to display. + break; + } + } + // A wishlist didn't have the field. + // If it's the last one, we want the variable to be false. + $wishlist = false; + } + } + + // Assume this wishlist template is rendering a live template. + // @todo Perhaps this should be controlled by a setting? + // "Show wishlist if current user has a wishlist connected to the template." + if ($wishlist !== false) { + // Remember, we're building a render array, so let's render this. + /** + * @var \Drupal\Core\Field\EntityReferenceFieldItemList $terms + * @var int $delta + * @var \Drupal\taxonomy\Entity\Term $term + */ + $terms = $wishlist_template->get('terms'); + $build['wishlist_template'] = array( + '#type' => 'container', + ); + foreach ($terms->referencedEntities() as $delta => $term) { + $build['wishlist_template']['tid-' . $term->id()] = $view_builder->view($term,$term_view_mode); + + // @todo Make the "No results" of the selected view actually work. + $product_list = array( + '#type' => 'view', + '#name' => $product_list_view, + '#display_id' => $product_list_display, + '#arguments' => array( + $wishlist->id(), + ), + ); + + // Create a list of Product variations whose parents have the catalog terms. + // Load all products based on variations + /** @var \Drupal\commerce_order\Entity\LineItemInterface[] $lineItems */ + $lineItems = $wishlist->getLineItems(); + // @todo Make this a static variable. + $terms_and_their_variations = array(); + foreach ($lineItems as $lineItem) { + /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation */ + $product_variation = $lineItem->getPurchasedEntity(); + /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ + $product = $product_variation->getProduct(); + $product_fields = array_keys($product->getFields()); + foreach ($product_fields as $product_field) { + $field = $product->get($product_field); + // Only interested in Entity References that target terms. + if ($field->getFieldDefinition()->getType() == "entity_reference" && + $field->getItemDefinition()->getSetting("target_type") == "taxonomy_term") { + foreach ($field->getValue() as $product_term) { + $terms_and_their_variations[$product_term['target_id']][] = $product_variation->id(); + } + } + } + } + + // If we have products that reference this term, render them. + if (in_array($term->id(),array_keys($terms_and_their_variations))) { + $product_list['#arguments'][1] = implode("+",$terms_and_their_variations[$term->id()]); + } + + // Render the view / no-results default + $build['wishlist_template']['tid-' . $term->id() . '---products'] = $product_list; + } + } else { + // This wishlist template is rendering a preview. + $build['wishlist_template'] = array( + '#type' => 'html_tag', + '#tag' => 'div', + '#attributes' => array( + 'class' => array( + 'wishlist-template-create' + ), + ), + '#value' => 'Start Shopping for ' . $wishlist_template->getName() . '', + ); + } +} From 897e710398dbb2a018033930a537c9863a9eb431 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Tue, 13 Sep 2016 19:41:48 -0400 Subject: [PATCH 10/12] WIP Issue #2794531: Wishlist Templates - Creating a wishlist template --- .../CreateWishlistBasedOnTemplateForm.php | 232 ++++++++++++++++++ wishlist_template/wishlist_template.module | 38 ++- 2 files changed, 248 insertions(+), 22 deletions(-) create mode 100644 wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php diff --git a/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php b/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php new file mode 100644 index 0000000..bdd9d90 --- /dev/null +++ b/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php @@ -0,0 +1,232 @@ +entityManager = $entity_manager; + $this->wishlistManager = $wishlist_manager; + $this->wishlistProvider = $wishlist_provider; + $this->orderTypeResolver = $order_type_resolver; + $this->storeContext = $store_context; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('commerce_wishlist.wishlist_manager'), + $container->get('commerce_wishlist.wishlist_provider'), + $container->get('commerce_order.chain_order_type_resolver'), + $container->get('commerce_store.store_context') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'wishlist_template_create_or_update'; + } + + /** + * Form constructor. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template + * + * @return array The form structure. + * The form structure. + */ + public function buildForm(array $form, FormStateInterface $form_state, $wishlist_template = NULL) { + + $form['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Start Shopping using this template.'), + ); + + return $form; + + } + + /** + * Form submission handler. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + /** + * @var \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template + * @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem[] $default_products + */ + $wishlist_template = $form_state->getBuildInfo()['args'][0]; + $default_products = $wishlist_template->get('default_products')->referencedEntities(); + if ($default_products) { + // Get/Create line item(s). + $purchased_entities = array(); + $line_items = array(); + foreach ($default_products as $product) { + /** + * @var \Drupal\commerce_product\Entity\ProductInterface $product + * @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation + * @var \Drupal\commerce_order\LineItemStorageInterface $line_item_storage + */ + $product_variation = $product->getDefaultVariation(); + $line_item_storage = $this->entityManager->getStorage('commerce_line_item'); + $purchased_entities[] = $product_variation; + $line_item = $line_item_storage->createFromPurchasableEntity($product_variation); + + // Now that the purchased entity is set, populate the title and price. + $line_item->setTitle($product_variation->getLineItemTitle()); + // @todo Remove once the price calculation is in place. + $line_item->unit_price = $product_variation->price; + $line_items[] = $line_item; + } + + // Use first default product for everything (facepalm). + $order_type = $this->orderTypeResolver->resolve($line_items[0]); + $store = $this->selectStore($purchased_entities[0]); + $wishlist = $this->wishlistProvider->getWishlist($order_type, $store); + if (!$wishlist) { + $wishlist = $this->wishlistProvider->createWishlist($order_type, $store); + } + + // Determine if wishlist has a field that can connect w/ template. + $wishlist_fields = array_keys($wishlist->getFields()); + foreach ($wishlist_fields as $wishlist_field) { + $order_wishlist_template_reference_field = $wishlist->get($wishlist_field); + // Only interested in Entity References that target wishlist_templates. + if ($order_wishlist_template_reference_field->getFieldDefinition()->getType() == "entity_reference" && + $order_wishlist_template_reference_field->getItemDefinition()->getSetting("target_type") == "wishlist_template") { + // The variable $field now has the field we will use to + // connect the template to the order. + break; + } + $order_wishlist_template_reference_field = FALSE; + } + + if ($order_wishlist_template_reference_field !== FALSE) { + // Connect wishlist to this template. + $wishlist->set($order_wishlist_template_reference_field->getName(),$wishlist_template->id()); + + // Add the default products (or increment them if they already exist. + foreach ($line_items as $line_item) { + $this->wishlistManager->addLineItem($wishlist, $line_item, TRUE); + } + drupal_set_message("Your wishlist is now using the " . $wishlist_template->getName() . "!"); + } else { + drupal_set_message($this->t('The order type ' . $order_type . ' must have an entity_reference that accepts a single wishlist_template.'),"error"); + } + } else { + drupal_set_message($this->t('Could not initiate your wishlist template because it has no default products.'),"error"); + } + + // TODO: Implement submitForm() method. + dpm("submission happened."); + } + + /** + * Selects the store for the given purchasable entity. + * + * If the entity is sold from one store, then that store is selected. + * If the entity is sold from multiple stores, and the current store is + * one of them, then that store is selected. + * + * @param \Drupal\commerce\PurchasableEntityInterface $entity + * The entity being added to cart. + * + * @throws \Exception + * When the entity can't be purchased from the current store. + * + * @return \Drupal\commerce_store\Entity\StoreInterface + * The selected store. + */ + protected function selectStore(PurchasableEntityInterface $entity) { + $stores = $entity->getStores(); + if (count($stores) === 1) { + $store = reset($stores); + } + else { + $store = $this->storeContext->getStore(); + if (!in_array($store, $stores)) { + // Indicates that the site listings are not filtered properly. + throw new \Exception("The given entity can't be purchased from the current store."); + } + } + + return $store; + } +} \ No newline at end of file diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module index 6e134a9..a60d63c 100644 --- a/wishlist_template/wishlist_template.module +++ b/wishlist_template/wishlist_template.module @@ -172,16 +172,18 @@ function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wi if ($wishlists) { // Look for wishlist template fields foreach ($wishlists as $wishlist) { - - $fields = array_keys($wishlist->toArray()); - if (in_array('field_test', $fields)) { - // Determine what template is assigned. - // @todo remove hardcoded Order entity requirement. - $values = $wishlist->get("field_test")->getValue(); - if ($values[0]['target_id'] == $wishlist_template->id()) { - // This order has the same value as the template we're viewing. - // Note that $wishlist will remain the order we want to display. - break; + $wishlist_fields = array_keys($wishlist->getFields()); + foreach ($wishlist_fields as $wishlist_field) { + $field = $wishlist->get($wishlist_field); + // Only interested in Entity References that target terms. + if ($field->getFieldDefinition()->getType() == "entity_reference" && + $field->getItemDefinition()->getSetting("target_type") == "wishlist_template") { + $values = $field->getValue(); + if ($values[0]['target_id'] == $wishlist_template->id()) { + // This order has the same value as the template we're viewing. + // Note that $wishlist will remain the order we want to display. + break 2; + } } } // A wishlist didn't have the field. @@ -207,7 +209,6 @@ function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wi foreach ($terms->referencedEntities() as $delta => $term) { $build['wishlist_template']['tid-' . $term->id()] = $view_builder->view($term,$term_view_mode); - // @todo Make the "No results" of the selected view actually work. $product_list = array( '#type' => 'view', '#name' => $product_list_view, @@ -241,7 +242,7 @@ function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wi } } - // If we have products that reference this term, render them. + // If we have product variation parents that reference this term, render them. if (in_array($term->id(),array_keys($terms_and_their_variations))) { $product_list['#arguments'][1] = implode("+",$terms_and_their_variations[$term->id()]); } @@ -250,16 +251,9 @@ function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wi $build['wishlist_template']['tid-' . $term->id() . '---products'] = $product_list; } } else { + $form = \Drupal::formBuilder()->getForm(\Drupal\wishlist_template\Form\CreateWishlistBasedOnTemplateForm::class, $wishlist_template); + // This wishlist template is rendering a preview. - $build['wishlist_template'] = array( - '#type' => 'html_tag', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'wishlist-template-create' - ), - ), - '#value' => 'Start Shopping for ' . $wishlist_template->getName() . '', - ); + $build['wishlist_template_form'] = $form; } } From ed8b05b052175a6489ffd500562c0f5267f3d534 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Tue, 27 Sep 2016 12:35:18 -0400 Subject: [PATCH 11/12] WIP Issue #2794531: Wishlist Templates - Moving to seperate module --- .../schema/wishlist_template_type.schema.yml | 12 - .../src/Entity/WishlistTemplate.php | 367 ------------------ .../src/Entity/WishlistTemplateInterface.php | 156 -------- .../src/Entity/WishlistTemplateType.php | 57 --- .../Entity/WishlistTemplateTypeInterface.php | 13 - .../src/Entity/WishlistTemplateViewsData.php | 28 -- .../CreateWishlistBasedOnTemplateForm.php | 232 ----------- .../src/Form/WishlistTemplateDeleteForm.php | 15 - .../src/Form/WishlistTemplateForm.php | 48 --- .../src/Form/WishlistTemplateSettingsForm.php | 55 --- .../Form/WishlistTemplateTypeDeleteForm.php | 53 --- .../src/Form/WishlistTemplateTypeForm.php | 67 ---- .../WishlistTemplateAccessControlHandler.php | 47 --- .../src/WishlistTemplateHtmlRouteProvider.php | 85 ---- .../src/WishlistTemplateListBuilder.php | 45 --- .../WishlistTemplateTypeHtmlRouteProvider.php | 59 --- .../src/WishlistTemplateTypeListBuilder.php | 32 -- ...shlist-template-content-add-list.html.twig | 23 -- .../templates/wishlist_template.html.twig | 22 -- wishlist_template/wishlist_template.info.yml | 10 - .../wishlist_template.links.action.yml | 11 - .../wishlist_template.links.menu.yml | 16 - .../wishlist_template.links.task.yml | 16 - wishlist_template/wishlist_template.module | 259 ------------ wishlist_template/wishlist_template.page.inc | 32 -- .../wishlist_template.permissions.yml | 22 -- 26 files changed, 1782 deletions(-) delete mode 100644 wishlist_template/config/schema/wishlist_template_type.schema.yml delete mode 100644 wishlist_template/src/Entity/WishlistTemplate.php delete mode 100644 wishlist_template/src/Entity/WishlistTemplateInterface.php delete mode 100644 wishlist_template/src/Entity/WishlistTemplateType.php delete mode 100644 wishlist_template/src/Entity/WishlistTemplateTypeInterface.php delete mode 100644 wishlist_template/src/Entity/WishlistTemplateViewsData.php delete mode 100644 wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php delete mode 100644 wishlist_template/src/Form/WishlistTemplateDeleteForm.php delete mode 100644 wishlist_template/src/Form/WishlistTemplateForm.php delete mode 100644 wishlist_template/src/Form/WishlistTemplateSettingsForm.php delete mode 100644 wishlist_template/src/Form/WishlistTemplateTypeDeleteForm.php delete mode 100644 wishlist_template/src/Form/WishlistTemplateTypeForm.php delete mode 100644 wishlist_template/src/WishlistTemplateAccessControlHandler.php delete mode 100644 wishlist_template/src/WishlistTemplateHtmlRouteProvider.php delete mode 100644 wishlist_template/src/WishlistTemplateListBuilder.php delete mode 100644 wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php delete mode 100644 wishlist_template/src/WishlistTemplateTypeListBuilder.php delete mode 100644 wishlist_template/templates/wishlist-template-content-add-list.html.twig delete mode 100644 wishlist_template/templates/wishlist_template.html.twig delete mode 100644 wishlist_template/wishlist_template.info.yml delete mode 100644 wishlist_template/wishlist_template.links.action.yml delete mode 100644 wishlist_template/wishlist_template.links.menu.yml delete mode 100644 wishlist_template/wishlist_template.links.task.yml delete mode 100644 wishlist_template/wishlist_template.module delete mode 100644 wishlist_template/wishlist_template.page.inc delete mode 100644 wishlist_template/wishlist_template.permissions.yml diff --git a/wishlist_template/config/schema/wishlist_template_type.schema.yml b/wishlist_template/config/schema/wishlist_template_type.schema.yml deleted file mode 100644 index 7c07e3d..0000000 --- a/wishlist_template/config/schema/wishlist_template_type.schema.yml +++ /dev/null @@ -1,12 +0,0 @@ -wishlist_template.wishlist_template_type.*: - type: config_entity - label: 'Wishlist template type config' - mapping: - id: - type: string - label: 'ID' - label: - type: label - label: 'Label' - uuid: - type: string diff --git a/wishlist_template/src/Entity/WishlistTemplate.php b/wishlist_template/src/Entity/WishlistTemplate.php deleted file mode 100644 index d3aa3ee..0000000 --- a/wishlist_template/src/Entity/WishlistTemplate.php +++ /dev/null @@ -1,367 +0,0 @@ - \Drupal::currentUser()->id(), - ); - } - - /** - * {@inheritdoc} - */ - public function getType() { - return $this->bundle(); - } - - /** - * {@inheritdoc} - */ - public function getName() { - return $this->get('name')->value; - } - - /** - * {@inheritdoc} - */ - public function setName($name) { - $this->set('name', $name); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getCreatedTime() { - return $this->get('created')->value; - } - - /** - * {@inheritdoc} - */ - public function setCreatedTime($timestamp) { - $this->set('created', $timestamp); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getOwner() { - return $this->get('user_id')->entity; - } - - /** - * {@inheritdoc} - */ - public function getOwnerId() { - return $this->get('user_id')->target_id; - } - - /** - * {@inheritdoc} - */ - public function setOwnerId($uid) { - $this->set('user_id', $uid); - return $this; - } - - /** - * {@inheritdoc} - */ - public function setOwner(UserInterface $account) { - $this->set('user_id', $account->id()); - return $this; - } - - /** - * {@inheritdoc} - */ - public function isPublished() { - return (bool) $this->getEntityKey('status'); - } - - /** - * {@inheritdoc} - */ - public function setPublished($published) { - $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getTaxonomyTermViewMode() { - return $this->get('taxonomy_term_view_mode')->value; - } - - /** - * {@inheritdoc} - */ - public function setTaxonomyTermViewMode($taxonomy_term_view_mode) { - $this->set('taxonomy_term_view_mode', $taxonomy_term_view_mode); - return $this; - } - - /** - * {@inheritdoc} - */ - public function setProductListRenderer($product_list_renderer) { - $this->set('product_list_renderer', $product_list_renderer); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getProductListRenderer() { - return $this->get('product_list_renderer')->value; - } - - /** - * {@inheritdoc} - */ - public function getTerms() { - return $this->get('terms')->value; - } - - /** - * {@inheritdoc} - */ - public function setTerms($terms) { - $this->set('terms', $terms); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getDefaultProducts() { - return $this->get('default_products')->value; - } - - /** - * {@inheritdoc} - */ - public function setDefaultProducts($terms) { - $this->set('default_products', $terms); - return $this; - } - - /** - * {@inheritdoc} - */ - public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields = parent::baseFieldDefinitions($entity_type); - - $fields['user_id'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Authored by')) - ->setDescription(t('The user ID of author of the Wishlist template entity.')) - ->setRevisionable(TRUE) - ->setSetting('target_type', 'user') - ->setSetting('handler', 'default') - ->setTranslatable(TRUE) - ->setDisplayOptions('view', array( - 'label' => 'hidden', - 'type' => 'author', - 'weight' => 0, - )) - ->setDisplayOptions('form', array( - 'type' => 'entity_reference_autocomplete', - 'weight' => 5, - 'settings' => array( - 'match_operator' => 'CONTAINS', - 'size' => '60', - 'autocomplete_type' => 'tags', - 'placeholder' => '', - ), - )) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - - $fields['name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Name')) - ->setRequired(TRUE) - ->setDescription(t('The name of the Wishlist template.')) - ->setSettings(array( - 'max_length' => 50, - 'text_processing' => 0, - )) - ->setDefaultValue('') - ->setDisplayOptions('view', array( - 'label' => 'above', - 'type' => 'string', - 'weight' => -4, - )) - ->setDisplayOptions('form', array( - 'type' => 'string_textfield', - 'weight' => -4, - )) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - - $fields['status'] = BaseFieldDefinition::create('boolean') - ->setLabel(t('Publishing status')) - ->setDescription(t('A boolean indicating whether the Wishlist template is published.')) - ->setDefaultValue(TRUE); - - $fields['created'] = BaseFieldDefinition::create('created') - ->setLabel(t('Created')) - ->setDescription(t('The time that the entity was created.')); - - $fields['changed'] = BaseFieldDefinition::create('changed') - ->setLabel(t('Changed')) - ->setDescription(t('The time that the entity was last edited.')); - - $fields['taxonomy_term_view_mode'] = BaseFieldDefinition::create('list_string') - ->setLabel(t('Category view mode')) - ->setDescription(t('The view mode to be used when rendering each category in the wishlist template.')) - ->setRequired(TRUE) - ->setTranslatable(TRUE) - ->setSettings([ - 'allowed_values_function' => '_wishlist_template_term_view_mode_values', - 'multiple' => false, - ]) - ->setDisplayOptions('form', [ - 'type' => 'select', - 'weight' => -3, - ]) - ->setDisplayOptions('view', [ - 'type' => 'hidden', - ]) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayConfigurable('form', TRUE); - - $fields['product_list_renderer'] = BaseFieldDefinition::create('list_string') - ->setLabel(t('View to be used when rendering products.')) - ->setDescription(t('On each template, we will render a taxonomy term and a - list of products that match the term. You can use the - supplied view or clone it and create your own to - quickly enable you to add a product image or sales - price formatter, or whatever this template might need. -

To be included in the above list, views are - required to be a base table of line items and have an - argument for order_id and purchaseable_entity.')) - ->setRequired(TRUE) - ->setTranslatable(TRUE) - ->setSettings([ - 'allowed_values_function' => '_wishlist_template_line_item_views_values', - 'multiple' => false, - ]) - ->setDisplayOptions('form', [ - 'type' => 'select', - 'weight' => -3, - ]) - ->setDisplayOptions('view', [ - 'type' => 'hidden', - ]) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayConfigurable('form', TRUE); - - $fields['terms'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Categories')) - ->setRequired(TRUE) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayConfigurable('form', TRUE) - ->setDescription(t('The categories used for grouping products the creating the wishlist template.')) - ->setSetting('target_type', 'taxonomy_term') - ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) - ->setDisplayOptions('form', [ - 'type' => 'entity_reference_autocomplete', - 'weight' => -2, - ]) - ->setDisplayOptions('view', [ - 'type' => 'hidden', - ]); - - $fields['default_products'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Default Products')) - ->setRequired(TRUE) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayConfigurable('form', TRUE) - ->setDescription(t('The product(s) that will be added to the wishlist - template on the wishlist creation. Note that we are - referencing the products, not the variations. The default - variation of this product will be added to the - wishlist. This does not currently work with other - types of product architectures.')) - ->setSetting('target_type', 'commerce_product') - ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) - ->setDisplayOptions('form', [ - 'type' => 'entity_reference_autocomplete', - 'weight' => -2, - ]) - ->setDisplayOptions('view', [ - 'type' => 'hidden', - ]); - - return $fields; - } - -} diff --git a/wishlist_template/src/Entity/WishlistTemplateInterface.php b/wishlist_template/src/Entity/WishlistTemplateInterface.php deleted file mode 100644 index 1ee7c46..0000000 --- a/wishlist_template/src/Entity/WishlistTemplateInterface.php +++ /dev/null @@ -1,156 +0,0 @@ - 'id', - 'title' => $this->t('Wishlist template'), - 'help' => $this->t('The Wishlist template ID.'), - ); - - return $data; - } - -} diff --git a/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php b/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php deleted file mode 100644 index bdd9d90..0000000 --- a/wishlist_template/src/Form/CreateWishlistBasedOnTemplateForm.php +++ /dev/null @@ -1,232 +0,0 @@ -entityManager = $entity_manager; - $this->wishlistManager = $wishlist_manager; - $this->wishlistProvider = $wishlist_provider; - $this->orderTypeResolver = $order_type_resolver; - $this->storeContext = $store_context; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager'), - $container->get('commerce_wishlist.wishlist_manager'), - $container->get('commerce_wishlist.wishlist_provider'), - $container->get('commerce_order.chain_order_type_resolver'), - $container->get('commerce_store.store_context') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'wishlist_template_create_or_update'; - } - - /** - * Form constructor. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * @param \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template - * - * @return array The form structure. - * The form structure. - */ - public function buildForm(array $form, FormStateInterface $form_state, $wishlist_template = NULL) { - - $form['submit'] = array( - '#type' => 'submit', - '#value' => $this->t('Start Shopping using this template.'), - ); - - return $form; - - } - - /** - * Form submission handler. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - /** - * @var \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template - * @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem[] $default_products - */ - $wishlist_template = $form_state->getBuildInfo()['args'][0]; - $default_products = $wishlist_template->get('default_products')->referencedEntities(); - if ($default_products) { - // Get/Create line item(s). - $purchased_entities = array(); - $line_items = array(); - foreach ($default_products as $product) { - /** - * @var \Drupal\commerce_product\Entity\ProductInterface $product - * @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation - * @var \Drupal\commerce_order\LineItemStorageInterface $line_item_storage - */ - $product_variation = $product->getDefaultVariation(); - $line_item_storage = $this->entityManager->getStorage('commerce_line_item'); - $purchased_entities[] = $product_variation; - $line_item = $line_item_storage->createFromPurchasableEntity($product_variation); - - // Now that the purchased entity is set, populate the title and price. - $line_item->setTitle($product_variation->getLineItemTitle()); - // @todo Remove once the price calculation is in place. - $line_item->unit_price = $product_variation->price; - $line_items[] = $line_item; - } - - // Use first default product for everything (facepalm). - $order_type = $this->orderTypeResolver->resolve($line_items[0]); - $store = $this->selectStore($purchased_entities[0]); - $wishlist = $this->wishlistProvider->getWishlist($order_type, $store); - if (!$wishlist) { - $wishlist = $this->wishlistProvider->createWishlist($order_type, $store); - } - - // Determine if wishlist has a field that can connect w/ template. - $wishlist_fields = array_keys($wishlist->getFields()); - foreach ($wishlist_fields as $wishlist_field) { - $order_wishlist_template_reference_field = $wishlist->get($wishlist_field); - // Only interested in Entity References that target wishlist_templates. - if ($order_wishlist_template_reference_field->getFieldDefinition()->getType() == "entity_reference" && - $order_wishlist_template_reference_field->getItemDefinition()->getSetting("target_type") == "wishlist_template") { - // The variable $field now has the field we will use to - // connect the template to the order. - break; - } - $order_wishlist_template_reference_field = FALSE; - } - - if ($order_wishlist_template_reference_field !== FALSE) { - // Connect wishlist to this template. - $wishlist->set($order_wishlist_template_reference_field->getName(),$wishlist_template->id()); - - // Add the default products (or increment them if they already exist. - foreach ($line_items as $line_item) { - $this->wishlistManager->addLineItem($wishlist, $line_item, TRUE); - } - drupal_set_message("Your wishlist is now using the " . $wishlist_template->getName() . "!"); - } else { - drupal_set_message($this->t('The order type ' . $order_type . ' must have an entity_reference that accepts a single wishlist_template.'),"error"); - } - } else { - drupal_set_message($this->t('Could not initiate your wishlist template because it has no default products.'),"error"); - } - - // TODO: Implement submitForm() method. - dpm("submission happened."); - } - - /** - * Selects the store for the given purchasable entity. - * - * If the entity is sold from one store, then that store is selected. - * If the entity is sold from multiple stores, and the current store is - * one of them, then that store is selected. - * - * @param \Drupal\commerce\PurchasableEntityInterface $entity - * The entity being added to cart. - * - * @throws \Exception - * When the entity can't be purchased from the current store. - * - * @return \Drupal\commerce_store\Entity\StoreInterface - * The selected store. - */ - protected function selectStore(PurchasableEntityInterface $entity) { - $stores = $entity->getStores(); - if (count($stores) === 1) { - $store = reset($stores); - } - else { - $store = $this->storeContext->getStore(); - if (!in_array($store, $stores)) { - // Indicates that the site listings are not filtered properly. - throw new \Exception("The given entity can't be purchased from the current store."); - } - } - - return $store; - } -} \ No newline at end of file diff --git a/wishlist_template/src/Form/WishlistTemplateDeleteForm.php b/wishlist_template/src/Form/WishlistTemplateDeleteForm.php deleted file mode 100644 index eef3703..0000000 --- a/wishlist_template/src/Form/WishlistTemplateDeleteForm.php +++ /dev/null @@ -1,15 +0,0 @@ -entity; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function save(array $form, FormStateInterface $form_state) { - $entity = $this->entity; - $status = parent::save($form, $form_state); - - switch ($status) { - case SAVED_NEW: - drupal_set_message($this->t('Created the %label Wishlist template.', [ - '%label' => $entity->label(), - ])); - break; - - default: - drupal_set_message($this->t('Saved the %label Wishlist template.', [ - '%label' => $entity->label(), - ])); - } - $form_state->setRedirect('entity.wishlist_template.canonical', ['wishlist_template' => $entity->id()]); - } - -} diff --git a/wishlist_template/src/Form/WishlistTemplateSettingsForm.php b/wishlist_template/src/Form/WishlistTemplateSettingsForm.php deleted file mode 100644 index abd4cbc..0000000 --- a/wishlist_template/src/Form/WishlistTemplateSettingsForm.php +++ /dev/null @@ -1,55 +0,0 @@ -t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); - } - - /** - * {@inheritdoc} - */ - public function getCancelUrl() { - return new Url('entity.wishlist_template_type.collection'); - } - - /** - * {@inheritdoc} - */ - public function getConfirmText() { - return $this->t('Delete'); - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $this->entity->delete(); - - drupal_set_message( - $this->t('content @type: deleted @label.', - [ - '@type' => $this->entity->bundle(), - '@label' => $this->entity->label(), - ] - ) - ); - - $form_state->setRedirectUrl($this->getCancelUrl()); - } - -} diff --git a/wishlist_template/src/Form/WishlistTemplateTypeForm.php b/wishlist_template/src/Form/WishlistTemplateTypeForm.php deleted file mode 100644 index 1bed548..0000000 --- a/wishlist_template/src/Form/WishlistTemplateTypeForm.php +++ /dev/null @@ -1,67 +0,0 @@ -entity; - $form['label'] = [ - '#type' => 'textfield', - '#title' => $this->t('Label'), - '#maxlength' => 255, - '#default_value' => $wishlist_template_type->label(), - '#description' => $this->t("Label for the Wishlist template type."), - '#required' => TRUE, - ]; - - $form['id'] = [ - '#type' => 'machine_name', - '#default_value' => $wishlist_template_type->id(), - '#machine_name' => [ - 'exists' => '\Drupal\wishlist_template\Entity\WishlistTemplateType::load', - ], - '#disabled' => !$wishlist_template_type->isNew(), - ]; - - /* You will need additional form elements for your custom properties. */ - - return $form; - } - - /** - * {@inheritdoc} - */ - public function save(array $form, FormStateInterface $form_state) { - $wishlist_template_type = $this->entity; - $status = $wishlist_template_type->save(); - - switch ($status) { - case SAVED_NEW: - drupal_set_message($this->t('Created the %label Wishlist template type.', [ - '%label' => $wishlist_template_type->label(), - ])); - break; - - default: - drupal_set_message($this->t('Saved the %label Wishlist template type.', [ - '%label' => $wishlist_template_type->label(), - ])); - } - $form_state->setRedirectUrl($wishlist_template_type->urlInfo('collection')); - } - -} diff --git a/wishlist_template/src/WishlistTemplateAccessControlHandler.php b/wishlist_template/src/WishlistTemplateAccessControlHandler.php deleted file mode 100644 index 54c1e97..0000000 --- a/wishlist_template/src/WishlistTemplateAccessControlHandler.php +++ /dev/null @@ -1,47 +0,0 @@ -isPublished()) { - return AccessResult::allowedIfHasPermission($account, 'view unpublished wishlist template entities'); - } - return AccessResult::allowedIfHasPermission($account, 'view published wishlist template entities'); - - case 'update': - return AccessResult::allowedIfHasPermission($account, 'edit wishlist template entities'); - - case 'delete': - return AccessResult::allowedIfHasPermission($account, 'delete wishlist template entities'); - } - - // Unknown operation, no opinion. - return AccessResult::neutral(); - } - - /** - * {@inheritdoc} - */ - protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { - return AccessResult::allowedIfHasPermission($account, 'add wishlist template entities'); - } - -} diff --git a/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php b/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php deleted file mode 100644 index 68d47dc..0000000 --- a/wishlist_template/src/WishlistTemplateHtmlRouteProvider.php +++ /dev/null @@ -1,85 +0,0 @@ -id(); - - if ($collection_route = $this->getCollectionRoute($entity_type)) { - $collection->add("entity.{$entity_type_id}.collection", $collection_route); - } - - if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) { - $collection->add("$entity_type_id.settings", $settings_form_route); - } - - return $collection; - } - - /** - * Gets the collection route. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type. - * - * @return \Symfony\Component\Routing\Route|null - * The generated route, if available. - */ - protected function getCollectionRoute(EntityTypeInterface $entity_type) { - if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { - $entity_type_id = $entity_type->id(); - $route = new Route($entity_type->getLinkTemplate('collection')); - $route - ->setDefaults([ - '_entity_list' => $entity_type_id, - '_title' => "{$entity_type->getLabel()} list", - ]) - ->setRequirement('_permission', 'access wishlist template overview') - ->setOption('_admin_route', TRUE); - - return $route; - } - } - - /** - * Gets the settings form route. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type. - * - * @return \Symfony\Component\Routing\Route|null - * The generated route, if available. - */ - protected function getSettingsFormRoute(EntityTypeInterface $entity_type) { - if (!$entity_type->getBundleEntityType()) { - $route = new Route("/admin/structure/{$entity_type->id()}/settings"); - $route - ->setDefaults([ - '_form' => 'Drupal\wishlist_template\Form\WishlistTemplateSettingsForm', - '_title' => "{$entity_type->getLabel()} settings", - ]) - ->setRequirement('_permission', $entity_type->getAdminPermission()) - ->setOption('_admin_route', TRUE); - - return $route; - } - } - -} diff --git a/wishlist_template/src/WishlistTemplateListBuilder.php b/wishlist_template/src/WishlistTemplateListBuilder.php deleted file mode 100644 index 0d99ae6..0000000 --- a/wishlist_template/src/WishlistTemplateListBuilder.php +++ /dev/null @@ -1,45 +0,0 @@ -t('Wishlist template ID'); - $header['name'] = $this->t('Name'); - return $header + parent::buildHeader(); - } - - /** - * {@inheritdoc} - */ - public function buildRow(EntityInterface $entity) { - /* @var $entity \Drupal\wishlist_template\Entity\WishlistTemplate */ - $row['id'] = $entity->id(); - $row['name'] = $this->l( - $entity->label(), - new Url( - 'entity.wishlist_template.edit_form', array( - 'wishlist_template' => $entity->id(), - ) - ) - ); - return $row + parent::buildRow($entity); - } - -} diff --git a/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php b/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php deleted file mode 100644 index b703e1c..0000000 --- a/wishlist_template/src/WishlistTemplateTypeHtmlRouteProvider.php +++ /dev/null @@ -1,59 +0,0 @@ -id(); - - if ($collection_route = $this->getCollectionRoute($entity_type)) { - $collection->add("entity.{$entity_type_id}.collection", $collection_route); - } - - return $collection; - } - - /** - * Gets the collection route. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type. - * - * @return \Symfony\Component\Routing\Route|null - * The generated route, if available. - */ - protected function getCollectionRoute(EntityTypeInterface $entity_type) { - if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { - $entity_type_id = $entity_type->id(); - $route = new Route($entity_type->getLinkTemplate('collection')); - $route - ->setDefaults([ - '_entity_list' => $entity_type_id, - // Make sure this is not a TranslatableMarkup object as the - // TitleResolver translates this string again. - '_title' => (string) $entity_type->getLabel(), - ]) - ->setRequirement('_permission', $entity_type->getAdminPermission()) - ->setOption('_admin_route', TRUE); - - return $route; - } - } - -} diff --git a/wishlist_template/src/WishlistTemplateTypeListBuilder.php b/wishlist_template/src/WishlistTemplateTypeListBuilder.php deleted file mode 100644 index 53a3e27..0000000 --- a/wishlist_template/src/WishlistTemplateTypeListBuilder.php +++ /dev/null @@ -1,32 +0,0 @@ -t('Wishlist template type'); - $header['id'] = $this->t('Machine name'); - return $header + parent::buildHeader(); - } - - /** - * {@inheritdoc} - */ - public function buildRow(EntityInterface $entity) { - $row['label'] = $entity->label(); - $row['id'] = $entity->id(); - // You probably want a few more properties here... - return $row + parent::buildRow($entity); - } - -} diff --git a/wishlist_template/templates/wishlist-template-content-add-list.html.twig b/wishlist_template/templates/wishlist-template-content-add-list.html.twig deleted file mode 100644 index ede5e34..0000000 --- a/wishlist_template/templates/wishlist-template-content-add-list.html.twig +++ /dev/null @@ -1,23 +0,0 @@ -{# -/** - * @file - * Default theme implementation to present a list of custom content entity types/bundles. - * - * Available variables: - * - types: A collection of all the available custom entity types/bundles. - * Each type/bundle contains the following: - * - link: A link to add a content entity of this type. - * - description: A description of this content entity types/bundle. - * - * @see template_preprocess_wishlist_template_content_add_list() - * - * @ingroup themeable - */ -#} -{% spaceless %} -
- {% for type in types %} -
{{ type.link }}
- {% endfor %} -
-{% endspaceless %} diff --git a/wishlist_template/templates/wishlist_template.html.twig b/wishlist_template/templates/wishlist_template.html.twig deleted file mode 100644 index 84a2d5c..0000000 --- a/wishlist_template/templates/wishlist_template.html.twig +++ /dev/null @@ -1,22 +0,0 @@ -{# -/** - * @file wishlist_template.html.twig - * Default theme implementation to present Wishlist template data. - * - * This template is used when viewing Wishlist template pages. - * - * - * Available variables: - * - content: A list of content items. Use 'content' to print all content, or - * - attributes: HTML attributes for the container element. - * - * @see template_preprocess_wishlist_template() - * - * @ingroup themeable - */ -#} - - {% if content %} - {{- content -}} - {% endif %} - diff --git a/wishlist_template/wishlist_template.info.yml b/wishlist_template/wishlist_template.info.yml deleted file mode 100644 index d438803..0000000 --- a/wishlist_template/wishlist_template.info.yml +++ /dev/null @@ -1,10 +0,0 @@ -name: Commerce Wishlist Template -type: module -description: Provides the ability to create product category lists that change the way a wishlist is created and presented. -package: Commerce (contrib) -core: 8.x -dependencies: - - commerce_wishlist - - entity_reference - - options - - entity diff --git a/wishlist_template/wishlist_template.links.action.yml b/wishlist_template/wishlist_template.links.action.yml deleted file mode 100644 index ec144d0..0000000 --- a/wishlist_template/wishlist_template.links.action.yml +++ /dev/null @@ -1,11 +0,0 @@ -entity.wishlist_template.add_form: - route_name: 'entity.wishlist_template.add_page' - title: 'Add Wishlist template' - appears_on: - - entity.wishlist_template.collection -entity.wishlist_template_type.add_form: - route_name: 'entity.wishlist_template_type.add_form' - title: 'Add Wishlist template type' - appears_on: - - entity.wishlist_template_type.collection - diff --git a/wishlist_template/wishlist_template.links.menu.yml b/wishlist_template/wishlist_template.links.menu.yml deleted file mode 100644 index e19578e..0000000 --- a/wishlist_template/wishlist_template.links.menu.yml +++ /dev/null @@ -1,16 +0,0 @@ -# Wishlist template menu items definition -entity.wishlist_template.collection: - title: 'Wishlist template list' - route_name: entity.wishlist_template.collection - description: 'List Wishlist template entities' - parent: 'commerce.admin_commerce' - weight: 100 - -# Wishlist template type menu items definition -entity.wishlist_template_type.collection: - title: 'Wishlist template type' - route_name: entity.wishlist_template_type.collection - description: 'List Wishlist template type (bundles)' - parent: 'commerce.configuration' - weight: 99 - diff --git a/wishlist_template/wishlist_template.links.task.yml b/wishlist_template/wishlist_template.links.task.yml deleted file mode 100644 index 44cfc09..0000000 --- a/wishlist_template/wishlist_template.links.task.yml +++ /dev/null @@ -1,16 +0,0 @@ -# Wishlist template routing definition -entity.wishlist_template.canonical: - route_name: entity.wishlist_template.canonical - base_route: entity.wishlist_template.canonical - title: 'View' - -entity.wishlist_template.edit_form: - route_name: entity.wishlist_template.edit_form - base_route: entity.wishlist_template.canonical - title: Edit - -entity.wishlist_template.delete_form: - route_name: entity.wishlist_template.delete_form - base_route: entity.wishlist_template.canonical - title: Delete - weight: 10 diff --git a/wishlist_template/wishlist_template.module b/wishlist_template/wishlist_template.module deleted file mode 100644 index a60d63c..0000000 --- a/wishlist_template/wishlist_template.module +++ /dev/null @@ -1,259 +0,0 @@ -' . t('About') . ''; - $output .= '

' . t('Description') . '

'; - return $output; - - default: - } -} - -/** - * Implements hook_theme(). - */ -function wishlist_template_theme() { - $theme = []; - $theme['wishlist_template'] = array( - 'render element' => 'elements', - 'file' => 'wishlist_template.page.inc', - 'template' => 'wishlist_template', - ); - $theme['wishlist_template_content_add_list'] = [ - 'render element' => 'content', - 'variables' => ['content' => NULL], - 'file' => 'wishlist_template.page.inc', - ]; - return $theme; -} - -/** -* Implements hook_theme_suggestions_HOOK(). -*/ -function wishlist_template_theme_suggestions_wishlist_template(array $variables) { - $suggestions = array(); - $entity = $variables['elements']['#wishlist_template']; - $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); - - $suggestions[] = 'wishlist_template__' . $sanitized_view_mode; - $suggestions[] = 'wishlist_template__' . $entity->bundle(); - $suggestions[] = 'wishlist_template__' . $entity->bundle() . '__' . $sanitized_view_mode; - $suggestions[] = 'wishlist_template__' . $entity->id(); - $suggestions[] = 'wishlist_template__' . $entity->id() . '__' . $sanitized_view_mode; - return $suggestions; -} - -/** - * Callback function that returns a list of taxonomy view modes. - */ -function _wishlist_template_term_view_mode_values() { - // Grab the View Mode service. - $entity_display_repository = \Drupal::service('entity_display.repository'); - - // The entity type for which we are looking. - $entity_type_id = "taxonomy_term"; - - // Use the service to get an array of available modes. - $view_modes = $entity_display_repository->getViewModes($entity_type_id); - - // Create a simple "allowed_values" array - $allowed_values = array(); - foreach ($view_modes as $id => $view_mode) { - $allowed_values[$id] = $view_mode['label'] . ' (' . $id . ')'; - } - - return $allowed_values; -} - -/** - * Create and return a list of views that are capable of rendering variations. - * - * Wishlist template sends the valid order id and a valid list of product ids as - * arguments (in that order) to the selected views from within the entity. - * - * - The base table MUST be 'commerce_line_item'. - * - The first argument MUST be 'order_id'. - * - This will be determined based on the url arguments. - * - The second argument MUST be 'purchased_entity' - * - Make sure you select 'allow multiple' otherwise this won't work as expected. - * - Code and two queries determine all product ids that should show up. - * - We are using Views purely as a very customizable list generator. - * - We have to do it this way to support any number of taxonomy fields and - * any kind of term (regardless of vocabulary). - * - * @return array - */ -function _wishlist_template_line_item_views_values() { - /** @var \Drupal\Core\Entity\Query\QueryFactory $query_factory */ - /** @var \Drupal\Core\Entity\Query\QueryInterface $views_query_interface */ - $query_factory = \Drupal::service('entity.query'); - $entity_manager = \Drupal::service('entity.manager'); - $views_query_interface = $query_factory->get('view'); - - // Grab all the line item views. - $views = $views_query_interface->condition('base_table','commerce_line_item')->execute(); - - // "Explode" based on display versions. - $wishlist_views = array(); - $views_default_args = array(); - foreach ($views as $view_id) { - /** @var \Drupal\views\Entity\View $view */ - $view = $entity_manager->getStorage('view')->load($view_id); - $view_displays = array_keys($view->get('display')); - foreach ($view_displays as $view_display) { - $display = $view->getDisplay($view_display); - $argument_keys = array_keys($display['display_options']['arguments']); - - // Track default arguments. See next step for why. - if ($view_display == "default") { - $views_default_args[$view_id] = $argument_keys; - } - - // Inherit default args if no argument keys are found. - if (empty($argument_keys) && !empty($views_default_args[$view_id])) { - $argument_keys = $views_default_args[$view_id]; - } - - // If this view has the appropriate args, return the info. - if (in_array("order_id", $argument_keys) && - in_array("purchased_entity", $argument_keys)) { - $combo_id = $view_id . '---' . $view_display; - $label = $view->label() . ': ' . $view_display; - $pseudo_machine_name = ' (' . $view_id . '.' . $view_display . ')'; - - // This view and display have the valid requirements to render product - // variations. - $wishlist_views[$combo_id] = $label . $pseudo_machine_name; - } - } - } - return $wishlist_views; -} - -/** - * Implements hook_ENTITY_TYPE_view_alter(). - * - * @param array $build - * @param \Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template - * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display - */ -function wishlist_template_wishlist_template_view_alter(array &$build, Drupal\wishlist_template\Entity\WishlistTemplateInterface $wishlist_template, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) { - $term_view_mode = $wishlist_template->getTaxonomyTermViewMode(); - $product_list_renderer = $wishlist_template->getProductListRenderer(); - $product_list_renderer = explode("---",$product_list_renderer); - $product_list_view = $product_list_renderer[0]; - $product_list_display = $product_list_renderer[1]; - $view_builder = \Drupal::entityManager()->getViewBuilder('taxonomy_term'); - /** - * @var \Drupal\commerce_wishlist\WishlistProviderInterface $wishlist_provider - * @var \Drupal\Core\Session\AccountProxy $current_account - * @var \Drupal\Core\Session\AccountProxy $current_account - **/ - $wishlist_provider = \Drupal::service('commerce_wishlist.wishlist_provider'); - $current_account = \Drupal::service('current_user'); - $wishlists = $wishlist_provider->getWishlists($current_account); - - /** @var \Drupal\commerce_order\Entity\OrderInterface $wishlist */ - $wishlist = false; - if ($wishlists) { - // Look for wishlist template fields - foreach ($wishlists as $wishlist) { - $wishlist_fields = array_keys($wishlist->getFields()); - foreach ($wishlist_fields as $wishlist_field) { - $field = $wishlist->get($wishlist_field); - // Only interested in Entity References that target terms. - if ($field->getFieldDefinition()->getType() == "entity_reference" && - $field->getItemDefinition()->getSetting("target_type") == "wishlist_template") { - $values = $field->getValue(); - if ($values[0]['target_id'] == $wishlist_template->id()) { - // This order has the same value as the template we're viewing. - // Note that $wishlist will remain the order we want to display. - break 2; - } - } - } - // A wishlist didn't have the field. - // If it's the last one, we want the variable to be false. - $wishlist = false; - } - } - - // Assume this wishlist template is rendering a live template. - // @todo Perhaps this should be controlled by a setting? - // "Show wishlist if current user has a wishlist connected to the template." - if ($wishlist !== false) { - // Remember, we're building a render array, so let's render this. - /** - * @var \Drupal\Core\Field\EntityReferenceFieldItemList $terms - * @var int $delta - * @var \Drupal\taxonomy\Entity\Term $term - */ - $terms = $wishlist_template->get('terms'); - $build['wishlist_template'] = array( - '#type' => 'container', - ); - foreach ($terms->referencedEntities() as $delta => $term) { - $build['wishlist_template']['tid-' . $term->id()] = $view_builder->view($term,$term_view_mode); - - $product_list = array( - '#type' => 'view', - '#name' => $product_list_view, - '#display_id' => $product_list_display, - '#arguments' => array( - $wishlist->id(), - ), - ); - - // Create a list of Product variations whose parents have the catalog terms. - // Load all products based on variations - /** @var \Drupal\commerce_order\Entity\LineItemInterface[] $lineItems */ - $lineItems = $wishlist->getLineItems(); - // @todo Make this a static variable. - $terms_and_their_variations = array(); - foreach ($lineItems as $lineItem) { - /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation */ - $product_variation = $lineItem->getPurchasedEntity(); - /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ - $product = $product_variation->getProduct(); - $product_fields = array_keys($product->getFields()); - foreach ($product_fields as $product_field) { - $field = $product->get($product_field); - // Only interested in Entity References that target terms. - if ($field->getFieldDefinition()->getType() == "entity_reference" && - $field->getItemDefinition()->getSetting("target_type") == "taxonomy_term") { - foreach ($field->getValue() as $product_term) { - $terms_and_their_variations[$product_term['target_id']][] = $product_variation->id(); - } - } - } - } - - // If we have product variation parents that reference this term, render them. - if (in_array($term->id(),array_keys($terms_and_their_variations))) { - $product_list['#arguments'][1] = implode("+",$terms_and_their_variations[$term->id()]); - } - - // Render the view / no-results default - $build['wishlist_template']['tid-' . $term->id() . '---products'] = $product_list; - } - } else { - $form = \Drupal::formBuilder()->getForm(\Drupal\wishlist_template\Form\CreateWishlistBasedOnTemplateForm::class, $wishlist_template); - - // This wishlist template is rendering a preview. - $build['wishlist_template_form'] = $form; - } -} diff --git a/wishlist_template/wishlist_template.page.inc b/wishlist_template/wishlist_template.page.inc deleted file mode 100644 index 85c52fd..0000000 --- a/wishlist_template/wishlist_template.page.inc +++ /dev/null @@ -1,32 +0,0 @@ - Date: Wed, 28 Sep 2016 14:51:48 -0400 Subject: [PATCH 12/12] WIP Issue #2794531: Wishlist Templates - Remove typo --- commerce_wishlist.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commerce_wishlist.module b/commerce_wishlist.module index 1c2184a..2e84641 100644 --- a/commerce_wishlist.module +++ b/commerce_wishlist.module @@ -316,7 +316,7 @@ function commerce_wishlist_add_to_wishlist_form_submit($form, FormStateInterface // Determine the order type to use. $order_type = $order_resolver->resolve($line_item); - // Find a store to use.git + // Find a store to use. $stores = $purchased_entity->getStores(); if (count($stores) === 1) { $store = reset($stores);