2

I want to create a Block-Module for drupal 8 (beta 12). To display the Content I want to use a twig-template (mytest-block.html.twig).

But the Twig-Template needs some Data to Display, So I need a Method to assign Some Variables from MytestBlock.php to the Template.

I know how to assign Variables from mytest.module, but I have no idea how to assign Variables from the Block-Plugin MytestBlock.php.

Or is it possible to access the instance of MytestBlock.php and therefore call a method which would return the variables as an array?

My MWE-Module consists of following files:

mytest/
├── mytest.info.yml
├── mytest.module
├── src
│   └── Plugin
│       └── Block
│           └── MytestBlock.php
└── templates
    └── mytest-block.html.twig

File Content mytest.info.yml:

name: mytest
description: A Testmodule
core: 8.x
package: Custom
version: 0.1
dependencies:
    - block
type: module

File Content mytest.module:

<?php

function mytest_theme() {
    return array('mytest_block' =>
        array('variables' => array('test' => 'hello')));
}

function template_preprocess_mytest_block(&$variables) {
    $variables["test2"] = "hello2";

    //a call to get_variables() would also be an option, e.g.:
    //$variables = array_merge($variables, $A_INSTANCE->get_variables());
}

File Content mytest-block.html.twig:

Template
{{ test }}
{{ test2 }}
{{ test3 }}
{{ test4 }}

File Content MytestBlock.php:

<?php

namespace Drupal\mytest\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;

/**
 * Provides a Test-Block
 *
 * @Block(
 *   id = "MytestBlock",
 *   admin_label = @Translation("My Testblock"),
 * )
 */
class MytestBlock extends BlockBasMytestBlock.phpe implements BlockPluginInterface {
  public function build() {
    return array(
        '#theme' => 'mytest_block',
        /*here I want to assign some variables: e.g.:
         * test3 = hello3 */
    );
  }

  public function get_variables() {
    retrun array("test4" => "hello4");
  }
}

In this example, only test and test2 are assigned, but I want, that test3 and test4 are assigned too.

1

Via try and error, I found out, that declaring the variables-array in the function mytest_theme() makes it possible, to set the variable in the MytestBlock-Class:

Changes in mytest.module:

function mytest_theme() {
    return array('mytest_block' =>
        array('variables' => array('test' => 'hello', 'test3' => null)));
}

Changes in MytestBlock.php:

class MytestBlock extends BlockBase implements BlockPluginInterface {
  public function build() {
    return array(
        '#theme' => 'mytest_block',
        '#test3' => 'hello3',
    );
  }
}

But I still have no idea how to access the Block-Plugin Instance in mytest.module.

  • Why do you still need the block (plugin) instance if you figured out how to pass through data directly? You can assign everything to #test, arrays, objects and access it in twig. The instance is not directly available there anymore. You could just make it available with #test => $this, but it's likely a lot easier to just add the data/info that you actually need to the render array. – Berdir Jul 27 '15 at 19:13
  • This is true, but I need the Instance in an other hook, other than mytest_theme(). – byteunit Jul 27 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.