Output category and chunkname

Hi!

Is there a way to output the category and name of e.g. a chunk on the finished page? This is primarily for educational use. There are some customers messing around with it, and it can be beneficial for all parties if they can see the result… :wink: And I can’t find it in the documentation for MODX.

Eg:
ChunkName: chk-something in Category cat-allthings

<div> <!-- Start of cat-allthings / chk-something -->
     ...
</div> <!-- End of cat-allthings / chk-something -->

And if it can be done, so that you can also turn it on and off via Systemsettings.

Many thanks in advance!

There has been a similar question some months ago:

The gist of it is, that it doesn’t seem to be possible.

A workaround is to manually add properties to the chunk with the relevant information. This could possibly be automated with a plugin (running for example on the OnChunkSave event).

I’m not sure what you want to do. If you want to display information on the chunks placed the current resource with chunk tags, it’s really tough.

You could do it with a plugin attached to an event that fires before the page is parsed (I think ON_LOAD_WEB_DOCUMENT might do it).

Then you’d search the text of the page for chunk tags, get the chunk name, get the category and its name, and add the info to the page text The feature could be turned on and off easily with a System Setting.

You could also find the chunk tags with JavaScript and call an AJAX processor to get the category names, or sent the page to the processor and let it find and return the information.

I don’t think this is what you want, but if you’d settle for a listing of all chunks by category, it would be possible (and I think easier) with a custom snippet. Here’s one way to start from scratch and display all chunks by category.

The steps would be something like this:

Query the db to get all categories sorted alphabetically by the category field (which holds the category name).

Loop though the retrieved categories. For each one, get the name of the category, then get all chunks in it (if any) with another query.
If there’s no chunk in that category, continue.
Otherwise, Add the Category name to the output.
Loop through the chunks, for each one.
---- Add the <div> tag to the output.
---- Add the chunk name to the output.
---- Add the </div> tag to the output.
When you’re out of chunks, go to the next category.

Thank you very much - unfortunately that was what I feared.

Hi Bobray,

And thanks for your reply!

My problem is that from time to time I teach MODX - not programming, but more frontend design and communication.
The target group for these courses are primarily graphic designers and journalists - and to be diplomatic - it is rare that they think particularly logically… However, they have learned a lot about HTML and CSS.

Therefore, it would be nice if they could see directly in the HTML source code where the individual chunks are located and their name as well as where they start and stop. Of course, they can indicate this themselves in a note <!-- Note --> in the individual chunks, but they forget that all the time… And it would be nice if there was an easier solution - both for them, but especially for me when debugging :wink:

As an example:

<section> <!-- Start of cat-aside-name/ chk-section-name -->
   <p>Content here</p>
<section> <!-- End of cat-aside-name / chk-section-name -->

Think, for example on a multi-level navigation that is generated by e.g. PDOtools, it can be reasonably difficult to understand for an inexperienced person.

Before, there was the Xhtmlbeautify plugin, which made it easier to view the source, but it has probably been abandoned.

But I’ll try to dive into your suggestions and see if I can work it out with your input!

I agree. It would be a great diagnostic tool. I’ve started working on something that might do the job.

OK, here it is. At several points I thought it was impossible, especially when I found commented-out code in the MODX classes from the multiple times I tried (and failed) to do it in the past. :wink:

The secret is to get the resource object from the DB, make the changes, and then assign it to $modx->resource. All points beyond that just ask if it exists, and parse it normally if it does. As a bonus, it highlights the chunk info, and makes the chunk name a link to edit the chunk in the Manager (assuming that you’re also logged in to the Manager).

Note that this will not identify chunks brought in by a snippet or plugin using $modx->getChunk(). Nor will it provide clues about text produced by snippets, though I think it could with some modifications.

<?php
/* Attach  plugin to OnWebPageInit.
   Create a Yes/No System setting 
   with the key: show_chunk_info
   to turn this on and off */
   
   
$showChunkInfo = $modx->getOption('show_chunk_info', null, true, true);

if (empty($showChunkInfo)) {
    return;
}


$pattern= '/\[\[[!|$]{1,2}([^]]+)]]/';


$styles = '<style>

.chunk_note {
  color: red;
  background-color:lightyellow;
}

.chunk_note > a {
  color: blue;
  text-decoration: underline;
}

.chunk_note > a:hover {
  background-color: gray;
}
</style>';

$modx->regClientCSS($styles);


$prefix = $modx->getVersionData()['version'] >= 3
    ? '\MODX\Revolution\\'
    : '';

$doc = $modx->getObject($prefix . 'modResource', $modx->resourceIdentifier);

$modx->resource = $doc;

$content = $doc->getContent();

$success = preg_match_all($pattern, $content, $matches);



if ($success) {
    $numMatches = count($matches[0]);
    for ($i = 0; $i < $numMatches; $i++) {
       $fullTag = $matches[0][$i];
       $chunkName = $matches[1][$i];
       $catName = 'None';

       if (strpos((string)$chunkName, ":") !== false) {
           $temp = explode(":", $chunkName);
           $chunkName = $temp[0];
       }
       $chunk = $modx->getObject($prefix . 'modChunk', 
           array('name' => $chunkName));
       if ($chunk) {
            $cat = $chunk->getOne('Category');
            if ($cat) {
               $catName = $cat->get('category');
            }
            $chunkId = $chunk->get('id');
       }
       
       $note = '<span class="chunk_note">[Chunk: ' . $chunkName . 
            ' -- Category: ' . $catName . ']</span>';
       $action = 'element/chunk/update&id=' . $chunkId;
       
       $link = '<a href="[[++manager_url]]?a=' . $action . 
            '" target="_blank">' . $chunkName . '</a>';
       
       $note = str_replace($chunkName, $link, $note);
       
       $content = str_replace($fullTag, $note . $fullTag, $content);
       
       $modx->resource->setContent($content);
      
    }
}

return;

Many thanks Bobray,

It works - well sort of: When using chunks in the templates it don’t - only when they are placed directly in the individual ressource it works…

There’s no easy way around that missing template info. If I have time, I’ll look into it. It’s easy enough to find chunk tags in the page’s template, but at the point where the plugin executes, they’re not in the page content any more, so placing the chunk info at the correct location could be unreliable, and in some cases, impossible. I could just list the chunks that appear in the template at the top of the page, but they wouldn’t be near the chunk content on the page.

I’ve solve the problem of showing chunks used in the template. It’s fairly complicated, so I’m doing a blog post on it for the MODX blog. It may be a little while before it shows up. It will come after one with an updated version of the code above, with an improved regex statement. Thanks again for the idea.

I am the one who has to say thank you for your great effort!

I’m working on an extra for this. I think I’ve solved the issue of chunks in the template. It’s complicated enough that it may not be totally reliable and may interfere with other plugins. It will probably be a while before it shows up, and ultimately, it may not work.