What is the right way to get a current topic?3 users found helpful
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).
It's more complicated in header_required and would probably require some reworking, it'd be good to think about how this might work.
I'd like to send pull request of this. How do you think?
I think this reactive page title avoids duplicate title tag problem.
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 188.8.131.52.
Like in a custom page-list template...
I can't figure out how to display the current topic in a portfolio project page.
$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...
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'));
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.
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->getTreeNodeDisplayName(); // print display name for first topic
Now presuming you want to list all the topics, with some markup here is a loop:
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.