0

Let's say I have 'opigno_documents_last_group_block' hook:

<!-- THEME DEBUG --> 
<!-- THEME HOOK: 'opigno_documents_last_group_block' -->
<!-- BEGIN OUTPUT from 'modules/contrib/opigno_learning_path/templates/opigno-documents-latest-group-block.html.twig' -->
<div class="content-box">
...

How can I enter it to pass some more variables to template?

I tried:

THEME_NAME_opigno_documents_last_group_block
THEME_NAME_opigno_documents_last_group_block_preprocess 
or
THEME_NAME_preprocess_block 

hooks and some others, but nothing seems to trigger this block, but it seems to dissapear (with plenty other blocks) when I unset npx_main_content_block.

The block opigno_documents_last_group_block is located in opigno_learning_path/src/Plugin/Block/DocumentsLastGroupBlock.php, which looks like this:

<?php

namespace Drupal\opigno_learning_path\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;

/**
 * Provides a documentslastgroupblock block.
 *
 * @Block(
 *   id = "opigno_documents_last_group_block",
 *   admin_label = @Translation("DocumentsLastGroupBlock"),
 *   category = @Translation("Custom")
 * )
 */
class DocumentsLastGroupBlock extends BlockBase {

  /**
   * @var \Drupal\Component\Plugin\Context\ContextInterface[]|mixed
   */
  protected $groupId;

  /**
   * {@inheritdoc}
   */
  public function build() {
    $this->groupId = $gid = $this->configuration["group"];
    $tid = _tft_get_group_tid($gid);
    $content = _tft_folder_content($tid, FALSE, $gid);
    $content = array_slice($content ?: [], 0, 4);
    foreach ($content as $index => $item) {
      $content[$index] = [
        '#theme' => 'opigno_documents_last_group_item',
        '#type' => $item["type"] == 'file' ? 'file' : 'folder',
        '#item' => $item,
        '#label' => $item["name"],
        '#link' => $this->itemLink($item),
      ];
    }
    $build['content'] = [
      '#theme' => 'opigno_documents_last_group_block',
      'content' => $content,
    ];
    return $build;
  }
(...)

and is attached in LearningPathController in this module:

 /**
  * Training document block.
  */
  public function trainingContentDocuments(&$content, $group) {

    // $TFTController = new TFTController();
    // $listGroup = $TFTController->listGroup($group->id()); 
    $tft_url = Url::fromRoute('tft.group', ['group' =>    $group->id()])->toString();

    $content['tabs'][] = $tft_url = [
     '#markup' => '<div class="see-all see-all-files"><a href="' . $tft_url . '">' . $this->t('See all') . '</a></div>',
    ];

    $block_render =    $this->attachBlock('opigno_documents_last_group_block', ['group' => $group->id()]);
   $block_render["content"]['link'] = $tft_url;
    $content['tab_content']['documents'] = (isset($block_render["content"]["content"]) && !empty($block_render["content"]["content"])) ? [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'documents',
      ],
      'block' => [
        'content' => $block_render["content"],
      ],
    ] : [];

  }
8
  • At least THEME_NAME_preprocess_block should work - did you cleared the cache and module/theme is enabled?
    – zanvidmar
    Feb 4, 2022 at 12:58
  • @zanvidmar of course, cache cleared, theme enabled&working
    – rukya
    Feb 4, 2022 at 13:02
  • Great and I guess you tried to use a debbugger and you created a breakpoint inside THEME_NAME_preprocess_block funtion and debugger did not stopped there, right?
    – zanvidmar
    Feb 4, 2022 at 13:04
  • for example, when I use this function: function npx_main_preprocess_block(&$variables) { kint($variables['attributes']['id']); } it prints only 4 block names and this one I am looking for is not there. It is like a part of other block, because it dissapears when I unset block-npx-main-content block
    – rukya
    Feb 4, 2022 at 13:21
  • Do you maybe know if this block is actually rendered inside this block? because if only some data is loaded I am not sure if preprocess hook is even triggered.
    – zanvidmar
    Feb 4, 2022 at 13:25

2 Answers 2

1

The correct name for the preprocess function is THEME_NAME_preprocess_opigno_documents_last_group_block().

4
  • wow, thanks so much, I was close, but I mismatched the order of words :) I ended up using hook_theme however, do you think that your solution is better choice?
    – rukya
    Feb 9, 2022 at 11:38
  • 1
    hook_theme and preprocess are different things. If you want just to make some updates to existing variables in the template (or add some new data to the template) then preprocess is a better choice. But if it is your custom (and programmatically created) block - then you can control your output in the build() function and add it in hook_theme. So: hoot_theme is good for custom blocks, and preprocesses are cool for things that are not programmatically created.
    – rsych
    Feb 9, 2022 at 12:07
  • 1
    Here you can see more info about custom blocks creation (and template with custom variables creation): drupal.org/docs/creating-custom-modules/creating-custom-blocks/…
    – rsych
    Feb 9, 2022 at 12:09
  • thanks a lot for explaining!
    – rukya
    Feb 10, 2022 at 10:57
0

I managed to understand how to solve my problem and pass extra fields using this hook:

function npx_main_theme() {
  return [
    'opigno_documents_last_group_block' => [
      'variables' => [
        'param1' => 'texttest'
      ]
    ]
  ];
}

Your Answer

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

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