You are here

class WeMegaMenuAdminController in Drupal Mega Menu 8

Same name and namespace in other branches
  1. 8.x src/Controller/WeMegaMenuAdminController.php \Drupal\we_megamenu\Controller\WeMegaMenuAdminController

Controller routines for block example routes.

Hierarchy

Expanded class hierarchy of WeMegaMenuAdminController

File

src/Controller/WeMegaMenuAdminController.php, line 16

Namespace

Drupal\we_megamenu\Controller
View source
class WeMegaMenuAdminController extends ControllerBase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @param \Drupal\Core\Extension\ModuleHandlerInterface
   *   The module handler.
   */
  protected $moduleHandler;

  /**
   * Constructs the WeMegaMenuAdminController.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'));
  }

  /**
   * A function build page backend.
   *
   * @param string $menu_name
   *   Public function configWeMegaMenu menu_name.
   *
   * @return array[markup]
   *   Public function configWeMegaMenu string.
   */
  public function configWeMegaMenu($menu_name) {

    // $tree = WeMegaMenuBuilder::getMenuTreeOrder($menu_name);
    $build = [];
    $build['we_megamenu'] = [
      '#theme' => 'we_megamenu_backend',
      '#menu_name' => $menu_name,
      // '#items' => $tree,
      '#blocks' => WeMegaMenuBuilder::getAllBlocks(),
      '#block_theme' => $this->configFactory
        ->get('system.theme')
        ->get('default'),
    ];
    $build['we_megamenu']['#attached']['library'][] = 'we_megamenu/form.we-mega-menu-backend';
    $abs_url_save_config = Url::fromRoute('we_megamenu.admin.save', [], [
      'absolute' => TRUE,
    ])
      ->toString();
    $abs_url_reset_config = Url::fromRoute('we_megamenu.admin.reset', [], [
      'absolute' => TRUE,
    ])
      ->toString();
    $abs_url_icons_config = Url::fromRoute('we_megamenu.geticons', [], [
      'absolute' => TRUE,
    ])
      ->toString();
    $build['#attached']['drupalSettings']['WeMegaMenu']['saveConfigWeMegaMenuURL'] = $abs_url_save_config;
    $build['#attached']['drupalSettings']['WeMegaMenu']['resetConfigWeMegaMenuURL'] = $abs_url_reset_config;
    $build['#attached']['drupalSettings']['WeMegaMenu']['iconsWeMegaMenuURL'] = $abs_url_icons_config;
    return $build;
  }

  /**
   * A function ajax save menu config.
   */
  public function saveConfigWeMegaMenu() {
    if (isset($_POST['action']) && $_POST['action'] == 'save') {
      $data_config = $_POST['data_config'];
      $theme = $_POST['theme'];
      $menu_name = $_POST['menu_name'];
      WeMegaMenuBuilder::saveConfig($menu_name, $theme, $data_config);
      we_megamenu_flush_render_cache();
    }
    exit;
  }

  /**
   * A function reset menu config.
   */
  public function resetConfigWeMegaMenu() {
    if (isset($_POST['action']) && $_POST['action'] == 'reset' && isset($_POST['menu_name']) && isset($_POST['theme'])) {
      $theme_array = WeMegaMenuBuilder::renderWeMegaMenuBlock($_POST['menu_name'], $_POST['theme']);
      $markup = render($theme_array);
      echo $markup;
      we_megamenu_flush_render_cache();
      exit;
    }
    if (isset($_POST['action']) && $_POST['action'] == 'reset-to-default' && isset($_POST['menu_name']) && isset($_POST['theme'])) {
      $query = \Drupal::database()
        ->delete('we_megamenu');
      $query
        ->condition('menu_name', $_POST['menu_name']);
      $query
        ->condition('theme', $_POST['theme']);
      $result = $query
        ->execute();
      WeMegaMenuBuilder::initMegamenu($_POST['menu_name'], $_POST['theme']);
      $theme_array = WeMegaMenuBuilder::renderWeMegaMenuBlock($_POST['menu_name'], $_POST['theme']);
      $markup = render($theme_array);
      echo $markup;
      we_megamenu_flush_render_cache();
      exit;
    }
    exit;
  }

  /**
   * A function set style backend.
   */
  public function styleOfBackendWeMegaMenu() {
    if (isset($_POST['type'])) {
      \Drupal::state()
        ->set('we_megamenu_backend_style', $_POST['type']);
      we_megamenu_flush_render_cache();
    }
    exit;
  }

  /**
   * Render block from post variable ajax.
   */
  public function renderBlock() {
    $title = TRUE;
    if ($_POST['title'] == 0) {
      $title = FALSE;
    }
    if (isset($_POST['bid']) && isset($_POST['section']) && !empty($_POST['bid'])) {
      echo WeMegaMenuBuilder::renderBlock($_POST['bid'], $title, isset($_POST['section']));
    }
    else {
      echo '';
    }
    exit;
  }

  /**
   * Render page list menu backend.
   */
  public function listWeMegaMenus() {
    $menus = menu_ui_get_menus();
    $rows = [];
    foreach ($menus as $name => $title) {
      $row = [
        'menu-name' => $name,
        'menu-title' => $title,
      ];
      $dropbuttons = [
        '#type' => 'operations',
        '#links' => [
          'config' => [
            'url' => new Url('we_megamenu.admin.configure', [
              'menu_name' => $name,
            ]),
            'title' => 'Config',
          ],
          'edit' => [
            'url' => new Url('entity.menu.edit_form', [
              'menu' => $name,
            ]),
            'title' => 'Edit links',
          ],
        ],
      ];
      $row['menu-operations'] = [
        'data' => $dropbuttons,
      ];
      $rows[] = $row;
    }
    $header = [
      'menu-machine-name' => t('Machine Name'),
      'menu-name' => t('Menu Name'),
      'menu-options' => t('Options'),
    ];
    return [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('No Drupal 8 Mega Menu block available. <a href="@link">Add Menu</a>.', [
        '@link' => Url::fromRoute('entity.menu.add_form')
          ->toString(),
      ]),
      '#attributes' => [
        'id' => 'we_megamenu',
      ],
    ];
  }

  /**
   * Render list icon font awesome.
   */
  public function getIcons() {
    $file = DRUPAL_ROOT . '/' . $this->moduleHandler
      ->getModule('we_megamenu')
      ->getPath() . '/assets/resources/icon.wemegamenu';
    $fh = fopen($file, 'r');
    $result = [];
    while ($line = fgets($fh)) {
      $result[] = trim($line);
    }
    fclose($fh);
    echo json_encode($result);
    exit;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WeMegaMenuAdminController::$configFactory protected property The config factory. Overrides ControllerBase::$configFactory
WeMegaMenuAdminController::$moduleHandler protected property The module handler. Overrides ControllerBase::$moduleHandler
WeMegaMenuAdminController::configWeMegaMenu public function A function build page backend.
WeMegaMenuAdminController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
WeMegaMenuAdminController::getIcons public function Render list icon font awesome.
WeMegaMenuAdminController::listWeMegaMenus public function Render page list menu backend.
WeMegaMenuAdminController::renderBlock public function Render block from post variable ajax.
WeMegaMenuAdminController::resetConfigWeMegaMenu public function A function reset menu config.
WeMegaMenuAdminController::saveConfigWeMegaMenu public function A function ajax save menu config.
WeMegaMenuAdminController::styleOfBackendWeMegaMenu public function A function set style backend.
WeMegaMenuAdminController::__construct public function Constructs the WeMegaMenuAdminController.