You are here

class AddToCart in Commerce Cart Flyout 8

Plugin implementation of the 'commerce_cart_flyout_add_to_cart' formatter.

Plugin annotation


@FieldFormatter(
  id = "commerce_cart_flyout_add_to_cart",
  label = @Translation("Flyout add to cart form"),
  field_types = {
    "entity_reference",
  },
)

Hierarchy

Expanded class hierarchy of AddToCart

1 file declares its use of AddToCart
commerce_cart_flyout.module in ./commerce_cart_flyout.module

File

src/Plugin/Field/FieldFormatter/AddToCart.php, line 36

Namespace

Drupal\commerce_cart_flyout\Plugin\Field\FieldFormatter
View source
class AddToCart extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The variation storage.
   *
   * @var \Drupal\commerce_product\ProductVariationStorageInterface
   */
  protected $variationStorage;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The serialize.
   *
   * @var \Symfony\Component\Serializer\Serializer
   */
  protected $serializer;

  /**
   * The attribute mapper.
   *
   * @var \Drupal\commerce_product\ProductVariationAttributeMapperInterface
   */
  protected $attributeMapper;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The attribute value view builder.
   *
   * @var \Drupal\Core\Entity\EntityViewBuilderInterface
   */
  protected $attributeValueViewBuilder;

  /**
   * The attribute value storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $attributeValueStorage;

  /**
   * The variation field renderer.
   *
   * @var \Drupal\commerce_product\ProductVariationFieldRendererInterface
   */
  protected $variationFieldRenderer;

  /**
   * The order item storage.
   *
   * @var \Drupal\commerce_order\OrderItemStorageInterface
   */
  protected $orderItemStorage;

  /**
   * Constructs a new AddToCart object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Symfony\Component\Serializer\Serializer $serializer
   *   The serializer.
   * @param \Drupal\commerce_product\ProductVariationAttributeMapperInterface $attribute_mapper
   *   The attribute mapper.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\commerce_product\ProductVariationFieldRendererInterface $variation_field_renderer
   *   The variation field renderer.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, Serializer $serializer, ProductVariationAttributeMapperInterface $attribute_mapper, RendererInterface $renderer, ProductVariationFieldRendererInterface $variation_field_renderer) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->variationStorage = $entity_type_manager
      ->getStorage('commerce_product_variation');
    $this->routeMatch = $route_match;
    $this->serializer = $serializer;
    $this->attributeMapper = $attribute_mapper;
    $this->renderer = $renderer;
    $this->attributeValueViewBuilder = $entity_type_manager
      ->getViewBuilder('commerce_product_attribute_value');
    $this->attributeValueStorage = $entity_type_manager
      ->getStorage('commerce_product_attribute_value');
    $this->variationFieldRenderer = $variation_field_renderer;
    $this->orderItemStorage = $entity_type_manager
      ->getStorage('commerce_order_item');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('entity_type.manager'), $container
      ->get('current_route_match'), $container
      ->get('serializer'), $container
      ->get('commerce_product.variation_attribute_mapper'), $container
      ->get('renderer'), $container
      ->get('commerce_product.variation_field_renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {

    /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
    $product = $items
      ->getEntity();

    // If we could not load a default variation, just bail.
    $default_variation = $this->variationStorage
      ->loadFromContext($product);
    if (!$default_variation) {
      return [];
    }
    $order_item = $this->orderItemStorage
      ->createFromPurchasableEntity($default_variation);
    $form_display = EntityFormDisplay::collectRenderDisplay($order_item, 'add_to_cart');
    $purchased_entity_widget = $form_display
      ->getComponent('purchased_entity');
    $variations = $this
      ->loadVariations($product);

    // Fake a requirement on the current route so that our Normalizers run.
    $this->routeMatch
      ->getRouteObject()
      ->setRequirement('_cart_api', 'true');
    $elements = [];
    $elements[0]['add_to_cart_form'] = [
      '#attached' => [
        'library' => [
          'core/drupalSettings',
          'commerce_cart_flyout/add_to_cart',
        ],
        'drupalSettings' => [
          'addToCart' => [
            $product
              ->uuid() => [
              'defaultVariation' => $default_variation
                ->uuid(),
              'variations' => $this->serializer
                ->normalize($variations),
              'injectedFields' => $this
                ->getVariationInjectedFields($variations),
              'type' => $purchased_entity_widget['type'],
            ],
          ],
          'theme' => [
            'commerce_cart_flyout_add_to_cart_button' => $this
              ->renderTemplate('commerce_cart_flyout_add_to_cart_button'),
            'commerce_cart_flyout_add_to_cart_variation_select' => $this
              ->renderTemplate('commerce_cart_flyout_add_to_cart_variation_select'),
          ],
        ],
      ],
      '#markup' => Markup::create(sprintf('<div %s></div>', new Attribute([
        'data-product' => $product
          ->uuid(),
        'data-view-mode' => $this->viewMode,
        'data-langcode' => $langcode,
      ]))),
    ];
    if ($purchased_entity_widget['type'] === 'commerce_product_variation_attributes') {
      foreach ($variations as $variation) {
        $prepared_attributes = $this->attributeMapper
          ->prepareAttributes($variation, $variations);
        $prepared_attributes = array_filter($prepared_attributes, static function (PreparedAttribute $prepared_attribute) {

          // There will always be at least one value, possibly `_none`.
          // If we have more than one value, allow the prepared attribute. But if
          // we only have one, do not consider it, if it is the `_none` value.
          $values = $prepared_attribute
            ->getValues();
          return count($values) > 1 || !isset($values['_none']);
        });
        $elements[0]['add_to_cart_form']['#attached']['library'][] = 'commerce_product/rendered-attributes';
        $elements[0]['add_to_cart_form']['#attached']['drupalSettings']['addToCart'][$product
          ->uuid()]['attributeOptions'][$variation
          ->uuid()] = [
          'attributes' => $this->serializer
            ->normalize(array_values($prepared_attributes)),
          'renderedAttributes' => $this
            ->renderPreparedAttributes($prepared_attributes),
        ];
      }
      $elements[0]['add_to_cart_form']['#attached']['drupalSettings']['theme'] += [
        'commerce_cart_flyout_add_to_cart_attributes_select' => $this
          ->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_select'),
        'commerce_cart_flyout_add_to_cart_attributes_radios' => $this
          ->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_radios'),
        'commerce_cart_flyout_add_to_cart_attributes_rendered' => $this
          ->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_rendered'),
      ];
    }
    $cacheability = new CacheableMetadata();
    $cacheability
      ->addCacheableDependency($order_item);
    $cacheability
      ->addCacheableDependency($form_display);
    $cacheability
      ->addCacheableDependency($product);
    foreach ($variations as $variation) {
      $cacheability
        ->addCacheableDependency($variation);
    }
    $cacheability
      ->applyTo($elements);
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    $has_cart = \Drupal::moduleHandler()
      ->moduleExists('commerce_cart');
    $entity_type = $field_definition
      ->getTargetEntityTypeId();
    $field_name = $field_definition
      ->getName();
    return $has_cart && $entity_type == 'commerce_product' && $field_name == 'variations';
  }

  /**
   * Filters prepared attributes by element type.
   *
   * @param array $prepared_attributes
   *   The prepared attributes.
   * @param string $element_type
   *   The element type to filter on.
   *
   * @return array
   *   An array of filter prepared attributes.
   */
  protected function filterPreparedAttributedByElementType(array $prepared_attributes, $element_type) {
    return array_filter($prepared_attributes, function (PreparedAttribute $attribute) use ($element_type) {
      return $attribute
        ->getElementType() == $element_type;
    });
  }

  /**
   * Renders the prepared attributes.
   *
   * @param array $prepared_attributes
   *   The prepared attributed.
   *
   * @return array
   *   The array of rendered prepated attributes.
   */
  protected function renderPreparedAttributes(array $prepared_attributes) {
    return array_map(function (PreparedAttribute $attribute) {
      return array_map(function ($attribute_value_id) {
        $attribute_value = $this->attributeValueStorage
          ->load($attribute_value_id);
        $attribute_value_build = $this->attributeValueViewBuilder
          ->view($attribute_value, 'add_to_cart');
        return [
          'output' => $this->renderer
            ->render($attribute_value_build),
          'attribute_value_id' => $attribute_value_id,
        ];
      }, array_keys($attribute
        ->getValues()));
    }, $this
      ->filterPreparedAttributedByElementType($prepared_attributes, 'commerce_product_rendered_attribute'));
  }

  /**
   * Load variations for the product.
   *
   * @param \Drupal\commerce_product\Entity\ProductInterface $product
   *   The product.
   *
   * @return \Drupal\commerce_product\Entity\ProductVariationInterface[]
   *   The variations keyed by UUID.
   */
  protected function loadVariations(ProductInterface $product) {
    return array_reduce($this->variationStorage
      ->loadEnabled($product), function ($carry, ProductVariationInterface $variation) {
      $carry[$variation
        ->uuid()] = $variation;
      return $carry;
    }, []);
  }

  /**
   * Renders a template.
   *
   * @param string $hook
   *   The theme hook.
   *
   * @return string
   *   The rendered template.
   */
  protected function renderTemplate($hook) {
    $build = [
      '#theme' => $hook,
    ];
    return $this->renderer
      ->renderPlain($build);
  }

  /**
   * Get injected variation fields.
   *
   * @param \Drupal\commerce_product\Entity\ProductVariationInterface[] $variations
   *   The variations.
   *
   * @return array
   *   The array of injected variation fields.
   */
  protected function getVariationInjectedFields(array $variations) {
    return array_map(function (ProductVariationInterface $variation) {
      return array_filter(array_map(function ($build) {
        return [
          'class' => $build['#ajax_replace_class'],
          'output' => trim($this->renderer
            ->render($build)),
        ];
      }, $this->variationFieldRenderer
        ->renderFields($variation, $this->viewMode)), function ($built) {
        return !empty($built['output']);
      });
    }, $variations);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddToCart::$attributeMapper protected property The attribute mapper.
AddToCart::$attributeValueStorage protected property The attribute value storage.
AddToCart::$attributeValueViewBuilder protected property The attribute value view builder.
AddToCart::$orderItemStorage protected property The order item storage.
AddToCart::$renderer protected property The renderer.
AddToCart::$routeMatch protected property The route match.
AddToCart::$serializer protected property The serialize.
AddToCart::$variationFieldRenderer protected property The variation field renderer.
AddToCart::$variationStorage protected property The variation storage.
AddToCart::create public static function Creates an instance of the plugin. Overrides FormatterBase::create
AddToCart::filterPreparedAttributedByElementType protected function Filters prepared attributes by element type.
AddToCart::getVariationInjectedFields protected function Get injected variation fields.
AddToCart::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterBase::isApplicable
AddToCart::loadVariations protected function Load variations for the product.
AddToCart::renderPreparedAttributes protected function Renders the prepared attributes.
AddToCart::renderTemplate protected function Renders a template.
AddToCart::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
AddToCart::__construct public function Constructs a new AddToCart object. Overrides FormatterBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::prepareView public function Allows formatters to load information for field values being displayed. Overrides FormatterInterface::prepareView 2
FormatterBase::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterInterface::settingsForm 24
FormatterBase::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterInterface::settingsSummary 22
FormatterBase::view public function Builds a renderable array for a fully themed field. Overrides FormatterInterface::view 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsInterface::defaultSettings 42
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.