What is the right way to get a current topic?

Permalink 3 users found helpful
The Page List filtering is great, but it is also the weakness of concrete5.
If a user click a link of topic tree, we should show which topic node is current, but we can't now.
So, I'd like to get the topic of current url once, then pass it to the several elements, like a title tag (header_required element), Page Title block, Breadcrumb Nav (Auto-Nav).
Any suggestions?

hissy
 
andrew replied on at Permalink Reply
andrew
This is actually something new that 5.7 provides. If you look at the sample content of the blog landing page, you can see that when you click the topic tree, both the topic block and the page list block react. The page list filters and the topic block highlights the current topic. That's because they both provide an "action_topic" method. I think that if you define an action_topic() method in the controller of the page title block, we could probably display the topic there.

It's more complicated in header_required and would probably require some reworking, it'd be good to think about how this might work.
hissy replied on at Permalink Reply 2 Attachments
hissy
Thank you for your advice! I've made a branch of this feature.

https://github.com/hissy/concrete5-5.7.0/commit/d59dc0c8d93feb7f27a4...

I'd like to send pull request of this. How do you think?
I think this reactive page title avoids duplicate title tag problem.

http://blog.raventools.com/the-most-common-seo-mistakes/...
andrew replied on at Permalink Reply
andrew
The modification to the page title block looks great. I think we'd approve that quickly.

The header_required.php snippet looks a little bit more brittle, though. I'm sure it works, but we don't usually like database calls directly in elements, and there's got to be a better way to change the title through a block action than just hard coding a single call like this. I definitely think there's a reason to want to do this, though. Maybe I can add a github issue for some functionality for this, and we could get it into 5.7.4, while we modify the page title block in time for 5.7.3.2.
hissy replied on at Permalink Reply
hissy
Thanks for the review. I'll create a pull request including a modification for page title only.
sthaigh replied on at Permalink Reply
I realize the topics documentation is on the roadmap however, Is there a method to get/display the current topic from the page object?

Like in a custom page-list template...
HardOne replied on at Permalink Reply
HardOne
I'd love to hear the answer, too.

I can't figure out how to display the current topic in a portfolio project page.
andrew replied on at Permalink Reply
andrew
If you just want to display the current topic of a page, do this from within your page or block template

$c = Page::getCurrentPage();
print $c->getAttribute('my_topic_handle', 'display');

where "my_topic_handle" is the handle of the topic attribute for the page. (e.g. "blog_entry_topics."). This will get a text string of selected topic names.

If you want to do more with topics, you can do

$topics = $c->getAttribute('my_topic_handle');

which will give you an array of \Concrete\Core\Tree\Node\Type\Topic objects that corresponds to any selected topics. You can loop through these, get their numerical IDs to use for searching, etc...
HardOne replied on at Permalink Reply
HardOne
Thank you andrew, but I don't get the text string of the current topic within a block template.

print $c->getAttribute('project_topics', 'display'); gives me "Array" as a text string.
var_dump($c->getAttribute('project_topics', 'display')); shows an array.
var_dump($c->getAttribute('project_topics')); shows almost the same array except for a different number behind the #.

Here my output from var_dump($c->getAttribute('project_topics', 'display'));
array(1) {
  [0]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#3839 (12) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "18"
    ["treeNodeTypeID"]=>


If I understand you right, in my case print $c->getAttribute('project_topics', 'display'); should print "Activities" instead of "Array". Does anyone have an idea what I'm doing wrong? Or does anyone have an idea how I can help myself, except for waiting for the documentation about topics?

Thank you very much.
goldhat replied on at Permalink Reply
Okay I see that the problem is the attribute value loads an array of the Topic objects rather than a text list. Which is good really because now you can output them however you want. You just have to use the method to retrieve the name.

See this API page to see what methods are available for these objectshttp://concrete5.org/api/class-Concrete.Core.Tree.Node.Type.Topic.h... Hint for anybody finding if you var_dump( $object ) whatever the namespace of the object is (in this case Concrete\Core\Tree\Node\Type\Topic) just put that in Google and the API page will show up as first result.

Test with this first which later we'll use in a loop:

$topics = $c->getAttribute('project_topics', 'display');
print $topics[0]->getTreeNodeDisplayName(); // print display name for first topic


Now presuming you want to list all the topics, with some markup here is a loop:

$topics = $c->getAttribute('project_topics', 'display');
foreach( $topics as $topic ) {
  print '<div class="topic">';
  print $topic->getTreeNodeDisplayName();
  print '</div>';
}
HardOne replied on at Permalink Reply
HardOne
Thank you so much goldhat!

Your code helps me a lot. I was able get get the current topics by array_map and implode $c->getAttribute('project_topics'), but I was sure that's not the right way. I know the API pages but I wasn't abled to connect things right.

With your example things start to make sense to me. Thank you for that. :)

I'm still confused why the attribute value loads an array of objects, when the parameter 'display' is given. I've searched the API pages and the code to take a look at getAttribute(), but I just don't get it. Maybe taking some time and some deeper thoughts will help.
sthaigh replied on at Permalink Reply
Bump! Karma points a takin...

Anyone?