1

I have a custom block module for Drupal 8. It is working on my localhost version of drupal (version 8.7.8). When I upload it to the web server (Version 8.7.11), I can enable the module, but it doesn't show up when I try to place the block on the block layout page. I don't have much control of the web server - files are uploaded via a git repository, but other modules I've added work without issues.

My module is just 2 files:

modules/custom/ischool_section_title_level_two/ischool_section_title_level_two.info.yml

name: iSchool Section Title Level Two
description: Provides a block that shows the Level Two title, or Level One if there is no Level Two.
core: 8.x
package: Custom
dependencies:
  - block
type: module

modules/custom/ischool_section_title_level_two/src/plugin/block/iSchoolSectionTitlelevel_two.php

<?php

namespace Drupal\ischool_section_title_level_two\Plugin\Block;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a block that shows the Level Two section title, or Level One title if there is no level Two
 *
 * @Block(
 *   id = "ischool_section_title_level_two",
 *   admin_label = @Translation("iSchool Section Title Level Two"),
 *   category = @Translation("Custom"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
 *   }
 * )
 */


//code adapted from http://hussainweb.me/an-easier-way-to-get-the-current-node-in-a-block-plugin-in-drupal-8/
//and https://design.briarmoon.ca/tutorials/drupal-8/getting-the-parent-node-of-a-drupal-8-node
class iSchoolSectionTitlelevel_two extends BlockBase {

  public function build() {
    $node = $this->getContextValue('node');
    if (empty($node))  {   
      return [
        '#markup' => "",
      ];
    }

    $L1_Title = $node->getTitle();
    $L2_Title = $node->getTitle();
    $currentNode = $node;

    while (true) {
      $parent_node = $this->getParentNode($currentNode);
      if (empty($parent_node)){
      break;
    }
      $L2_Title = $L1_Title;
      $L1_Title = $parent_node->getTitle();

      $currentNode = $parent_node;
   }

    return [
      '#markup' => $L2_Title,
    ];
  }

  private function getParentNode($node){


    if (empty($node)) return null;
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $links = $menu_link_manager->loadLinksByRoute('entity.node.canonical', ['node' => $node->id()]);

    // Because loadLinksByRoute() returns an array keyed by a complex id
    // it is simplest to just get the first result by using array_pop().
    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $link = array_pop($links);
    if (empty($link)) return null;



    /** @var \Drupal\Core\Menu\MenuLinkInterface $parent */
     if ($link->getParent() && $parent = $menu_link_manager->createInstance($link->getParent())) {
        if (!method_exists($parent, "getUrlObject")) return null;
        $urlObj = $parent->getUrlObject();
        if (is_null($urlObj)) return null;
        if (!method_exists($urlObj, "getRouteParameters")) return null;
        $route = $urlObj->getRouteParameters();
        if (empty($route)) return null;
        if (!isset($route['node'])) return null;
        $parent_node = \Drupal::entityManager()->getStorage('node')->load($route['node']);
        return $parent_node;
     }
     else return null;
  }

  // cache this block for a definite time.
  public function getCacheMaxAge() {
    return 43200;
  }



}

1 Answer 1

2

This was an issue with the capitalization of the folders.

The 2nd file should have been in the /src/Plugin/Block/ folder but instead was in the /src/plugin/block/ folder (missing the initial caps).

On the local windows machine, this didn't make any difference. On the LAMP stack machine it resulted in the block not showing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.