You are here

public function AddtoCartWithQuantityFormatter::viewElements in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddToCartWithQuantityFormatter::viewElements()
  2. 8.0 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartWithQuantityFormatter::viewElements()
  3. 8.2 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartWithQuantityFormatter::viewElements()
  4. 8.3 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartWithQuantityFormatter::viewElements()
  5. 8.4 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartWithQuantityFormatter::viewElements()
  6. 8.5 src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartWithQuantityFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/AddToCartWithQuantityFormatter.php, line 45

Class

AddtoCartWithQuantityFormatter
Plugin implementation of the 'addtocartwithquantity' formatter.

Namespace

Drupal\basic_cart\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $config = \Drupal::config('basic_cart.settings');

  // $id			= $config->get('quantity_status') ? '\Drupal\basic_cart\Form\AddToCartWithQuantity' : '\Drupal\basic_cart\Form\AddToCart';
  // $id     = '\Drupal\basic_cart\Form\AddToCartWithQuantity';
  $addtocart_wrapper_container_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_wrapper_container_class'))
    ->__toString();
  $addtocart_button_container_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_button_container_class'))
    ->__toString();
  $addtocart_button_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_button_class'))
    ->__toString();
  $addtocart_message_wrapper_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_message_wrapper_class'))
    ->__toString();
  $addtocart_quantity_wrapper_container_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_quantity_wrapper_container_class'))
    ->__toString();
  $addtocart_quantity_textfield_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_quantity_textfield_class'))
    ->__toString();
  $addtocart_quantity_label_class = SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_quantity_label_class'))
    ->__toString();
  $addtocart_quantity_label_value = t(SafeMarkup::checkPlain($this
    ->getSetting('quantity_addtocart_quantity_label_value'))
    ->__toString());
  $entity = $items
    ->getEntity();
  $config = \Drupal::config('basic_cart.settings');
  $elements = array();
  $option = [
    'query' => [
      'entitytype' => $entity
        ->getEntityTypeId(),
      'quantity' => '',
    ],
    'absolute' => TRUE,
  ];
  if (trim($config
    ->get('add_to_cart_redirect')) != "<none>" && trim($config
    ->get('add_to_cart_redirect')) != "") {
    $url = Url::fromRoute('basic_cart.cartadddirect', [
      "nid" => $entity
        ->id(),
    ], $option);
    $link = '<a id="forquantitydynamictext_' . $entity
      ->id() . '" class="basic_cart-get-quantity button ' . $addtocart_button_class . '" href="' . $url
      ->toString() . '">' . $this
      ->t($config
      ->get('add_to_cart_button')) . '</a>';
  }
  else {
    $url = Url::fromRoute('basic_cart.cartadd', [
      "nid" => $entity
        ->id(),
    ], $option);
    $link = '<a id="forquantitydynamictext_' . $entity
      ->id() . '" class="basic_cart-get-quantity button use-basic_cart-ajax ' . $addtocart_button_class . '" href="' . $url
      ->toString() . '">' . $this
      ->t($config
      ->get('add_to_cart_button')) . '</a>';
  }

  /*$link_options = [
      'attributes' => [
        'class' => [
          'basic_cart-get-quantity',
          'use-basic_cart-ajax',
          'button',
        ],
      ],
    ];
    $url->setOptions($link_options);*/
  $quantity_content = $config
    ->get('quantity_status') ? '<div id="quantity-wrapper_' . $entity
    ->id() . '" class="addtocart-quantity-wrapper-container ' . $addtocart_quantity_wrapper_container_class . '"></div>' : '';

  //$link = new Link($this->t($config->get('add_to_cart_button')),$url);
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => 'ajax-addtocart-wrapper ' . $addtocart_message_wrapper_class,
        'id' => 'ajax-addtocart-message-' . $entity
          ->id(),
      ],
      '#prefix' => '<div class="addtocart-wrapper-container ' . $addtocart_wrapper_container_class . '">' . $quantity_content . '<div class="addtocart-link-class ' . $addtocart_button_container_class . '">' . $link . "</div>",
      '#suffix' => '</div>',
    ];
  }
  $elements['#attached']['library'][] = 'core/drupal.ajax';
  $elements['#attached']['drupalSettings']['basic_cart']['textfield_class'] = $addtocart_quantity_textfield_class;
  $elements['#attached']['drupalSettings']['basic_cart']['label_class'] = $addtocart_quantity_label_class;
  $elements['#attached']['drupalSettings']['basic_cart']['label_value'] = $addtocart_quantity_label_value;
  return $elements;
}