You are here

public function SuperfishBlock::build in Superfish 8

Implements \Drupal\block\BlockBase::build().

Overrides SystemMenuBlock::build

File

src/Plugin/Block/SuperfishBlock.php, line 1135

Class

SuperfishBlock
Provides a "Superfish" block.

Namespace

Drupal\superfish\Plugin\Block

Code

public function build() {
  $build = [];

  // Block settings which will be passed to the Superfish themes.
  $sfsettings = [];
  $sfsettings['level'] = $this->configuration['level'];
  $sfsettings['depth'] = $this->configuration['depth'];
  $sfsettings['menu_type'] = $this->configuration['menu_type'];
  $sfsettings['style'] = $this->configuration['style'];
  $sfsettings['expanded'] = $this->configuration['expanded'];
  $sfsettings['itemdepth'] = $this->configuration['link_depth_class'];
  $sfsettings['ulclass'] = $this->configuration['custom_list_class'];
  $sfsettings['liclass'] = $this->configuration['custom_item_class'];
  $sfsettings['hlclass'] = $this->configuration['custom_link_class'];
  $sfsettings['clone_parent'] = $this->configuration['clone_parent'];
  $sfsettings['hide_linkdescription'] = $this->configuration['hide_linkdescription'];
  $sfsettings['add_linkdescription'] = $this->configuration['add_linkdescription'];
  $sfsettings['multicolumn'] = $this->configuration['multicolumn'];
  $sfsettings['multicolumn_depth'] = $this->configuration['menu_type'] == 'navbar' && $this->configuration['multicolumn_depth'] == 1 ? 2 : $this->configuration['multicolumn_depth'];
  $sfsettings['multicolumn_levels'] = $this->configuration['multicolumn_levels'] + $sfsettings['multicolumn_depth'];

  // jQuery plugin options which will be passed to the Drupal behavior.
  $sfoptions = [];
  $sfoptions['pathClass'] = $sfsettings['menu_type'] == 'navbar' ? 'active-trail' : '';
  $sfoptions['pathLevels'] = $this->configuration['pathlevels'] != 1 ? $this->configuration['pathlevels'] : '';
  $sfoptions['delay'] = $this->configuration['delay'] != 800 ? $this->configuration['delay'] : '';
  $sfoptions['animation']['opacity'] = 'show';
  $slide = $this->configuration['slide'];
  if (strpos($slide, '_')) {
    $slide = explode('_', $slide);
    switch ($slide[1]) {
      case 'vertical':
        $sfoptions['animation']['height'] = [
          'show',
          $slide[0],
        ];
        break;
      case 'horizontal':
        $sfoptions['animation']['width'] = [
          'show',
          $slide[0],
        ];
        break;
      case 'diagonal':
        $sfoptions['animation']['height'] = [
          'show',
          $slide[0],
        ];
        $sfoptions['animation']['width'] = [
          'show',
          $slide[0],
        ];
        break;
    }
    $build['#attached']['library'][] = 'superfish/superfish_easing';
  }
  else {
    switch ($slide) {
      case 'vertical':
        $sfoptions['animation']['height'] = 'show';
        break;
      case 'horizontal':
        $sfoptions['animation']['width'] = 'show';
        break;
      case 'diagonal':
        $sfoptions['animation']['height'] = 'show';
        $sfoptions['animation']['width'] = 'show';
        break;
    }
  }
  $speed = $this->configuration['speed'];
  if ($speed != 'normal') {
    if (is_numeric($speed)) {
      $sfoptions['speed'] = (int) $speed;
    }
    elseif (in_array($speed, [
      'slow',
      'normal',
      'fast',
    ])) {
      $sfoptions['speed'] = $speed;
    }
  }
  if ($this->configuration['arrow'] == 0) {
    $sfoptions['autoArrows'] = FALSE;
  }
  if ($this->configuration['shadow'] == 0) {
    $sfoptions['dropShadows'] = FALSE;
  }
  if ($this->configuration['hoverintent']) {
    $build['#attached']['library'][] = 'superfish/superfish_hoverintent';
  }
  else {
    $sfoptions['disableHI'] = TRUE;
  }
  $sfoptions = superfish_array_filter($sfoptions);

  // Options for Superfish sub-plugins.
  $sfplugins = [];
  $touchscreen = $this->configuration['touch'];
  if ($touchscreen) {
    $build['#attached']['library'][] = 'superfish/superfish_touchscreen';
    $behaviour = $this->configuration['touchbh'];
    $sfplugins['touchscreen']['behaviour'] = $behaviour != 2 ? $behaviour : '';
    switch ($touchscreen) {
      case 1:
        $sfplugins['touchscreen']['mode'] = 'always_active';
        break;
      case 2:
        $sfplugins['touchscreen']['mode'] = 'window_width';
        $tsbp = $this->configuration['touchbp'];
        $sfplugins['touchscreen']['breakpoint'] = $tsbp != 768 ? (double) $tsbp : '';
        break;
      case 3:

        // Which method to use for UA detection.
        $tsuam = $this->configuration['touchuam'];
        $tsua = $this->configuration['touchua'];
        switch ($tsuam) {

          // Client-side.
          case 0:
            switch ($tsua) {
              case 0:
                $sfplugins['touchscreen']['mode'] = 'useragent_predefined';
                break;
              case 1:
                $sfplugins['touchscreen']['mode'] = 'useragent_custom';
                $tsual = drupal_strtolower($this->configuration['touchual']);
                if (strpos($tsual, '*')) {
                  $tsual = str_replace('*', '|', $tsual);
                }
                $sfplugins['touchscreen']['useragent'] = $tsual;
                break;
            }
            break;

          // Server-side.
          case 1:
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
              $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
              switch ($tsua) {

                // Use the pre-defined list of mobile UA strings.
                case 0:
                  if (preg_match('/(android|bb\\d+|meego)|avantgo|bada\\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
                    $sfplugins['touchscreen']['mode'] = 'always_active';
                    if ($behaviour == 2) {
                      $sfsettings['clone_parent'] = 1;
                    }
                  }
                  break;

                // Use the custom list of UA strings.
                case 1:
                  $tsual = drupal_strtolower($this->configuration['touchual']);
                  $tsuac = [];
                  if (strpos($tsual, '*')) {
                    $tsual = explode('*', $tsual);
                    foreach ($tsual as $ua) {
                      $tsuac[] = strpos($hua, $ua) ? 1 : 0;
                    }
                  }
                  else {
                    $tsuac[] = strpos($hua, $tsual) ? 1 : 0;
                  }
                  if (in_array(1, $tsuac)) {
                    $sfplugins['touchscreen']['mode'] = 'always_active';
                    if ($behaviour == 2) {
                      $sfsettings['clone_parent'] = 1;
                    }
                  }
                  break;
              }
            }
            break;
        }
        break;
    }
  }
  $smallscreen = $this->configuration['small'];
  if ($smallscreen) {
    $build['#attached']['library'][] = 'superfish/superfish_smallscreen';
    switch ($smallscreen) {
      case 1:
        $sfplugins['smallscreen']['mode'] = 'always_active';
        break;
      case 2:
        $sfplugins['smallscreen']['mode'] = 'window_width';
        $ssbp = $this->configuration['smallbp'];
        if ($ssbp != 768) {
          $sfplugins['smallscreen']['breakpoint'] = (double) $ssbp;
        }
        else {
          $sfplugins['smallscreen']['breakpoint'] = '';
        }
        break;
      case 3:

        // Which method to use for UA detection.
        $ssuam = $this->configuration['smalluam'];
        $ssua = $this->configuration['smallua'];
        switch ($ssuam) {

          // Client-side.
          case 0:
            switch ($ssua) {
              case 0:
                $sfplugins['smallscreen']['mode'] = 'useragent_predefined';
                break;
              case 1:
                $sfplugins['smallscreen']['mode'] = 'useragent_custom';
                $ssual = drupal_strtolower($this->configuration['smallual']);
                if (strpos($ssual, '*')) {
                  $ssual = str_replace('*', '|', $ssual);
                }
                $sfplugins['smallscreen']['useragent'] = $ssual;
                break;
            }
            break;

          // Server-side.
          case 1:
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
              $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
              switch ($ssua) {

                // Use the pre-defined list of mobile UA strings.
                case 0:
                  if (preg_match('/(android|bb\\d+|meego).+mobile|avantgo|bada\\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
                    $sfplugins['smallscreen']['mode'] = 'always_active';
                  }
                  break;

                // Use the custom list of UA strings.
                case 1:
                  $ssual = $this->configuration['smallual'];
                  $ssual = drupal_strtolower($ssual);
                  $ssuac = [];
                  if (strpos($ssual, '*')) {
                    $ssual = explode('*', $ssual);
                    foreach ($ssual as $ua) {
                      $ssuac[] = strpos($hua, $ua) ? 1 : 0;
                    }
                  }
                  else {
                    $ssuac[] = strpos($hua, $ssual) ? 1 : 0;
                  }
                  if (in_array(1, $ssuac)) {
                    $sfplugins['smallscreen']['mode'] = 'always_active';
                  }
                  break;
              }
            }
            break;
        }
        break;
    }
    $type = $this->configuration['smallact'];
    switch ($type) {
      case 0:
        $asa = $this->configuration['smallasa'];
        $cmc = $this->configuration['smallcmc'];
        $chc = $this->configuration['smallchc'];
        $ecm = $this->configuration['smallecm'];
        $ech = $this->configuration['smallech'];
        $icm = $this->configuration['smallicm'];
        $ich = $this->configuration['smallich'];
        $sfplugins['smallscreen']['type'] = 'select';
        $sfplugins['smallscreen']['addSelected'] = $asa == 1 ? TRUE : '';
        $sfplugins['smallscreen']['menuClasses'] = $cmc == 1 ? TRUE : '';
        if ($chc == 1) {
          $sfplugins['smallscreen']['hyperlinkClasses'] = TRUE;
        }
        if ($cmc == 1 && !empty($ecm)) {
          $sfplugins['smallscreen']['excludeClass_menu'] = $ecm;
        }
        if ($chc == 1 && !empty($ech)) {
          $sfplugins['smallscreen']['excludeClass_hyperlink'] = $ech;
        }
        if (!empty($icm)) {
          $sfplugins['smallscreen']['includeClass_menu'] = $icm;
        }
        if (!empty($ich)) {
          $sfplugins['smallscreen']['includeClass_hyperlink'] = $ich;
        }
        break;
      case 1:
        $ab = $this->configuration['smallabt'];
        $sfplugins['smallscreen']['accordionButton'] = $ab != 1 ? $ab : '';
        if ($this
          ->t('Expand') != 'Expand') {
          $sfplugins['smallscreen']['expandText'] = $this
            ->t('Expand');
        }
        if ($this
          ->t('Collapse') != 'Collapse') {
          $sfplugins['smallscreen']['collapseText'] = $this
            ->t('Collapse');
        }
        break;
    }
  }
  if ($this->configuration['supposition']) {
    $sfplugins['supposition'] = TRUE;
    $build['#attached']['library'][] = 'superfish/superfish_supposition';
  }
  if ($this->configuration['supersubs']) {
    $build['#attached']['library'][] = 'superfish/superfish_supersubs';
    $minwidth = $this->configuration['minwidth'];
    $maxwidth = $this->configuration['maxwidth'];
    $sfplugins['supersubs']['minWidth'] = $minwidth != 12 ? $minwidth : '';
    $sfplugins['supersubs']['maxWidth'] = $maxwidth != 27 ? $maxwidth : '';
    if (empty($sfplugins['supersubs']['minWidth']) && empty($sfplugins['supersubs']['maxWidth'])) {
      $sfplugins['supersubs'] = TRUE;
    }
  }

  // Attaching the requires JavaScript and CSS files.
  $build['#attached']['library'][] = 'superfish/superfish';
  if ($sfsettings['style'] != 'none') {
    $style = 'superfish/superfish_style_' . $sfsettings['style'];
    $build['#attached']['library'][] = $style;
  }

  // Title for the small-screen menu.
  if ($smallscreen) {
    $title = '';
    switch ($type) {
      case 0:
        $title = $this->configuration['smallset'];
        break;
      case 1:
        $title = $this->configuration['smallamt'];
        break;
    }
    $sfplugins['smallscreen']['title'] = $title ? $title : $this
      ->label();
  }
  $sfplugins = superfish_array_filter($sfplugins);

  // Menu block ID.
  $menu_name = $this
    ->getDerivativeId();

  // Menu tree.
  $level = $this->configuration['level'];

  // Menu display depth.
  $depth = $sfsettings['depth'];

  /*
   * By not setting the any expanded parents we don't limit the loading of the
   * subtrees.
   * Calling MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters we
   * would be doing so.
   * We don't actually need the parents expanded as we do different rendering.
   */
  if ($depth) {
    $maxdepth = min($level + ($depth - 1), $this->menuTree
      ->maxDepth());
  }
  else {
    $maxdepth = NULL;
  }
  $parameters = (new MenuTreeParameters())
    ->setMinDepth($level)
    ->setMaxDepth($maxdepth)
    ->setActiveTrail($this->menuActiveTrail
    ->getActiveTrailIds($menu_name))
    ->onlyEnabledLinks();

  // For menu blocks with start level greater than 1, only show menu items
  // from the current active trail. Adjust the root according to the current
  // position in the menu in order to determine if we can show the subtree.
  if ($level > 1) {
    if (count($parameters->activeTrail) >= $level) {

      // Active trail array is child-first. Reverse it, and pull the new menu
      // root based on the parent of the configured start level.
      $menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
      $menu_root = $menu_trail_ids[$level - 1];
      $parameters
        ->setRoot($menu_root)
        ->setMinDepth(1);
      if ($depth > 0) {
        $parameters
          ->setMaxDepth(min($level - 1 + $depth - 1, $this->menuTree
          ->maxDepth()));
      }
    }
    else {
      return [];
    }
  }
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);

  // Unique HTML ID.
  $id = Html::getUniqueId('superfish-' . $menu_name);

  // Preparing the Drupal behavior.
  $build['#attached']['drupalSettings']['superfish'][$id]['id'] = $id;
  if (isset($sfoptions)) {
    $build['#attached']['drupalSettings']['superfish'][$id]['sf'] = $sfoptions;
  }
  else {
    $build['#attached']['drupalSettings']['superfish'][$id]['sf'] = [];
  }
  if (!empty($sfplugins)) {
    $build['#attached']['drupalSettings']['superfish'][$id]['plugins'] = $sfplugins;
  }

  // Calling the theme.
  $build['content'] = [
    '#theme' => 'superfish',
    '#menu_name' => $menu_name,
    '#html_id' => $id,
    '#tree' => $tree,
    '#settings' => $sfsettings,
  ];

  // Build the original menu tree to calculate cache tags and contexts.
  $treeBuild = $this->menuTree
    ->build($tree);
  $build['#cache'] = $treeBuild['#cache'];
  return $build;
}