You are here

public function AddtoCartFormatter::viewElements in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddToCartFormatter::viewElements()
  2. 8.0 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartFormatter::viewElements()
  3. 8.2 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartFormatter::viewElements()
  4. 8.3 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartFormatter::viewElements()
  5. 8.4 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartFormatter::viewElements()
  6. 8.5 src/Plugin/Field/FieldFormatter/AddToCartFormatter.php \Drupal\basic_cart\Plugin\Field\FieldFormatter\AddtoCartFormatter::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/AddToCartFormatter.php, line 40

Class

AddtoCartFormatter
Plugin implementation of the 'addtocart' formatter.

Namespace

Drupal\basic_cart\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entity = $items
    ->getEntity();

  //SafeMarkup::checkPlain($value)->__toString();
  $addtocart_wrapper_container_class = SafeMarkup::checkPlain($this
    ->getSetting('addtocart_wrapper_container_class'))
    ->__toString();
  $addtocart_button_container_class = SafeMarkup::checkPlain($this
    ->getSetting('addtocart_button_container_class'))
    ->__toString();
  $addtocart_button_class = SafeMarkup::checkPlain($this
    ->getSetting('addtocart_button_class'))
    ->__toString();
  $addtocart_message_wrapper_class = SafeMarkup::checkPlain($this
    ->getSetting('addtocart_message_wrapper_class'))
    ->__toString();
  $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)*/

  //$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 . '"><div class="addtocart-link-class ' . $addtocart_button_container_class . '">' . $link . "</div>",
      '#suffix' => '</div>',
    ];
  }
  $elements['#attached']['library'][] = 'core/drupal.ajax';

  //$form['#attached']['drupalSettings']['contactinfo']['sitename'] = $drupalConfig->get('name');
  return $elements;
}