You are here

public function CartBlock::build in Commerce Cart API 8

Same name in this branch
  1. 8 tests/modules/commerce_cart_js/src/Plugin/Block/CartBlock.php \Drupal\commerce_cart_js\Plugin\Block\CartBlock::build()
  2. 8 tests/modules/commerce_cart_reactjs/src/Plugin/Block/CartBlock.php \Drupal\commerce_cart_reactjs\Plugin\Block\CartBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

tests/modules/commerce_cart_js/src/Plugin/Block/CartBlock.php, line 23

Class

CartBlock
Provides a cart block.

Namespace

Drupal\commerce_cart_js\Plugin\Block

Code

public function build() {
  $registry = \Drupal::getContainer()
    ->get('theme.registry')
    ->get();
  $cart_block_theme = $registry['commerce_cart_js_block'];
  $twig_theme_registry = \Drupal::getContainer()
    ->get('twig.loader.theme_registry');
  $twig = $twig_theme_registry
    ->getSourceContext($cart_block_theme['template'] . '.html.twig');
  return [
    '#attached' => [
      'library' => [
        'commerce_cart_js/cart',
      ],
      'drupalSettings' => [
        'cartBlock' => [
          'template' => $twig
            ->getCode(),
          'context' => [
            'url' => Url::fromRoute('commerce_cart.page')
              ->toString(),
            'icon' => file_create_url(drupal_get_path('module', 'commerce') . '/icons/ffffff/cart.png'),
          ],
        ],
      ],
    ],
    '#markup' => Markup::create('<div id="commerce_cart_js_block"></div>'),
  ];
}