Commit 3726594e authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Ryan Szrama
Browse files

Issue #3203891 by khiminrm, jsacksick, rszrama: Addressbook integration when gprofile is enabled

parent abfdbb70
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -8,8 +8,21 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\gcommerce\Plugin\GroupContentEnabler\GroupProfile;
use Drupal\group\Entity\GroupContentType;

/**
 * Implements hook_group_content_info_alter().
 */
function gcommerce_group_content_info_alter(array &$definitions) {
  foreach ($definitions as $key => $definition) {
    if (strpos($key, 'group_profile:') !== 0) {
      continue;
    }
    $definitions[$key]['class'] = GroupProfile::class;
  }
}

/**
 * Implements hook_ENTITY_TYPE_access() for commerce_store.
 */

src/AddressBook.php

0 → 100644
+110 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\gcommerce;

use Drupal\commerce_order\AddressBook as CoreAddressBook;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\group\GroupMembershipLoaderInterface;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\user\UserInterface;

class AddressBook extends CoreAddressBook {

  /**
   * The membership loader service.
   *
   * @var \Drupal\group\GroupMembershipLoaderInterface
   */
  protected $membershipLoader;

  /**
   * Constructs a new AddressBook object.
   *
   * @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info
   *   The entity type bundle info.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\group\GroupMembershipLoaderInterface $membership_loader
   *   The group membership loader service.
   */
  public function __construct(EntityTypeBundleInfo $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, GroupMembershipLoaderInterface $membership_loader) {
    parent::__construct($entity_type_bundle_info, $entity_type_manager);

    $this->membershipLoader = $membership_loader;
  }

  /**
   * {@inheritdoc}
   */
  public function loadAll(UserInterface $customer, $profile_type_id, array $available_countries = []) {
    $profiles = [];
    $list_only_group_profiles = FALSE;
    // Load the profiles belonging to groups the given customer is member of.
    foreach ($this->membershipLoader->loadByUser($customer) as $group_membership) {
      try {
        $group = $group_membership->getGroup();
        // Check whether only profiles belonging to groups should be returned
        // by the Addressbook.
        $configuration = $group->getGroupType()->getContentPlugin("group_profile:$profile_type_id")->getConfiguration();
        // @todo: Check what to do when profiles from different group types
        // with different configuration is returned.
        if (!empty($configuration['addressbook_list_group_profiles'])) {
          $list_only_group_profiles = TRUE;
        }
        foreach ($group->getContentEntities("group_profile:$profile_type_id") as $profile) {
          if (!$this->isAvailable($profile, $available_countries)) {
            continue;
          }
          $profiles[$profile->id()] = $profile;
        }
      }
      catch (PluginNotFoundException $exception) {
        continue;
      }
    }

    // Skip returning profiles belonging to the current customer if configured
    // to do so.
    if (!$list_only_group_profiles) {
      $profiles += parent::loadAll(
        $customer,
        $profile_type_id,
        $available_countries
      );
    }

    return $profiles;
  }

  /**
   * {@inheritdoc}
   */
  public function copy(ProfileInterface $profile, UserInterface $customer) {
    parent::copy($profile, $customer);
    // Add the addressbook profile to customer's groups.
    if ($profile->getData('address_book_profile_id')) {
      $addressbook_profile = $this->profileStorage->load($profile->getData('address_book_profile_id'));

      // @todo Add functionality that will load allowed groups.
      // Allowed groups for which can be added card entity.
      $allowedGroupTypeIds = ['company'];

      $groups = $this->membershipLoader->loadByUser($customer);
      foreach ($groups as $membership) {
        $group = $membership->getGroup();
        $groupType = $group->getGroupType();
        if (!in_array($groupType->id(), $allowedGroupTypeIds)) {
          continue;
        }
        $contentPluginId = 'group_profile:' . $addressbook_profile->bundle();
        if ($groupType->hasContentPlugin($contentPluginId)
          && empty($group->getContentByEntityId($contentPluginId, $addressbook_profile->id()))) {
          $group->addContent($profile, $contentPluginId);
        }
      }
    }
  }

}
+28 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\gcommerce;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;

class GcommerceServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    // We cannot use the module handler as the container is not yet compiled.
    // @see \Drupal\Core\DrupalKernel::compileContainer()
    $modules = $container->getParameter('container.modules');

    // Swap the addressbook service with our own if both commerce_order and
    // the gprofile modules are enabled.
    if (isset($modules['commerce_order'], $modules['gprofile'])) {
      $container->getDefinition('commerce_order.address_book')
        ->setClass(AddressBook::class)
        ->addArgument(new Reference('group.membership_loader'));
    }
  }

}
+38 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\gcommerce\Plugin\GroupContentEnabler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\gprofile\Plugin\GroupContentEnabler\GroupProfile as GroupProfileBase;

/**
 * Extends the group_profile group content enabler with additional settings.
 */
class GroupProfile extends GroupProfileBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'addressbook_list_group_profiles' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    $form['addressbook_list_group_profiles'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Addressbook: List only group profiles'),
      '#description' => $this->t('Whether the addressbook should list only customer profiles belonging to groups the customer is a member of.'),
      '#default_value' => $this->configuration['addressbook_list_group_profiles'],
    ];

    return $form;
  }

}