You are here

TBMegaMenuController.php in The Better Mega Menu 8

Same filename and directory in other branches
  1. 2.x src/Controller/TBMegaMenuController.php

File

src/Controller/TBMegaMenuController.php
View source
<?php

namespace Drupal\tb_megamenu\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Security\TrustedCallbackInterface;

/**
 * Handler for attaching columns to MegaMenu render arrays.
 */
class TBMegaMenuController extends ControllerBase implements TrustedCallbackInterface {

  /**
   * Attach the number of columns into JS.
   *
   * @throws \Exception
   */
  public static function tbMegamenuAttachNumberColumns($childrens, $elements) {
    $number_columns =& drupal_static('column');
    $render_array = [];
    $render_array['#attached']['drupalSettings']['TBMegaMenu'] = [
      'TBElementsCounter' => [
        'column' => $number_columns,
      ],
    ];

    // Can't use DI here since it's invoked by the static method below.
    \Drupal::service('renderer')
      ->render($render_array);
    return $childrens;
  }

  /**
   * {@inheritDoc}
   */
  public static function trustedCallbacks() {
    return [
      'tbMegamenuAttachNumberColumns',
    ];
  }

}

Classes

Namesort descending Description
TBMegaMenuController Handler for attaching columns to MegaMenu render arrays.