Score:0

Can I use theme hook to display some extra fields in block?

id flag

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"],
      ],
    ] : [];

  }
zanvidmar avatar
sa flag
At least THEME_NAME_preprocess_block should work - did you cleared the cache and module/theme is enabled?
rukya avatar
id flag
@zanvidmar of course, cache cleared, theme enabled&working
zanvidmar avatar
sa flag
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?
rukya avatar
id flag
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
zanvidmar avatar
sa flag
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.
rukya avatar
id flag
@zanvidmar thanks for helping! it is irendered nside contrib_module/src/Plugin/Block, if that makes sense
zanvidmar avatar
sa flag
Can you share a little bit more of that code, not sure I am getting the whole picture here, you can even update the question itself. tnx
rukya avatar
id flag
@zanvidmar I've updated the info in question
Score:1
ru flag

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

rukya avatar
id flag
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?
ru flag
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.
ru flag
Here you can see more info about custom blocks creation (and template with custom variables creation): https://www.drupal.org/docs/creating-custom-modules/creating-custom-blocks/create-a-custom-block
rukya avatar
id flag
thanks a lot for explaining!
Score:0
id flag

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'
      ]
    ]
  ];
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.