I'm new to Drupal development and created a new very basic custom module similar to what you find here https://github.com/xaiwant/drupal8-block/tree/master/article .

One of my content types includes a taxonomy which allows to choose between a bunch of predefined HTML segments stored in the taxonomy description fields.

Now I would like to access this description field content of the current node from my custom block, but I have no clue how to access the proper variables.

class ArticleBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    return array(
      '#type' => 'markup',
      '#markup' => '>>>HERE I NEED THE TAXONOMY HTML<<<',
    );
  }
}

I installed the devel module to figure out the available variables,
but when I try to call dmp() or similar I only get "unexpected errors" :-( 

So any help of someone who could point me too the right direction would really be appreciated...

Ingo

Comments

wombatbuddy’s picture

For exploring variables in Drupal 8 you can use kint().
See 'How to Print Variables using Kint in Drupal 8'.
Here are the links that also may be useful:
'Drupal 8 Entity API cheat sheet'
'Working with the Entity API'.

inki’s picture

Thanks for these links; I am now able to actually dump $this using ism in the block definition.

However - I'm missing a reference to the current node.
How can I get data from my current node or page?

wombatbuddy’s picture

inki’s picture

That's it. Great. I guess I'll find my way through now.

Thanks a lot for your assistance.  😃👍

inki’s picture

Everything seemed to be right, but for some reason I am fetching the wrong -always the same- taxonomy item.
Within my block I first fetch the current node:

    $node = \Drupal::routeMatch()->getParameter('node');

This one is correct according to ksm($node)

Then I try to access the Taxonomy term of  a field named "field_ag" that belongs to my content type.
This field is of type entity reference.

    $term = Term::load($node->get('field_ag')->target_id);

This always refers to the same taxonomy :-( 

So what is my mistake here?

ALSO: I found that deleting the cache might change the icon - why that??

inki’s picture

Actually is was the default caching of the block, disabled by adding the following function:

  public function getCacheMaxAge() {
    return 0;
  }