Adding a block_var array to another block_var array

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Adding a block_var array to another block_var array

Post by Kailey »

I'm trying to append data to each topicrow in viewforum. Currently, the following code works.

PHP:

Code: Select all

$topic_row = array_merge($topic_row, array(
    'TOPIC_TAGS' => implode($this->user->lang('COMMA_SEPARATOR'), $tags),
));
HTML:

Code: Select all

<!-- IF topicrow.TOPIC_TAGS -->
<div class="tag">{topicrow.TOPIC_TAGS}</div>
<!-- ENDIF -->
What I would like is to add a second block_var array to $topicrow, but I'm not sure how to proceed. I've tried the following with no success.

PHP:

Code: Select all

foreach ($tags as tag)
{
    $tags_row = $this->assign_block_vars('topicrow.tagrow', array(
        'TOPIC_TAGS' => $tag,
    ));
}

$topic_row = array_merge($topic_row, $tags_row);
HTML:

Code: Select all

<!-- IF topicrow.tagrow.TOPIC_TAGS -->
<!-- BEGIN topicrow.tagrow -->
<div class="tag">{topicrow.TOPIC_TAGS}</div>
<!-- END topicrow.tagrow -->
<!-- ENDIF -->
I know I'm doing something wrong, but this is above my head right now. Can someone help please?
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Adding a block_var array to another block_var array

Post by RMcGirr83 »

core.viewforum_topic_row_after :?:
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Adding a block_var array to another block_var array

Post by Kailey »

Thank you for the suggestion, but I'm not sure it will work. It has to be within the topicrow block_var, not after. That event is triggered after

Code: Select all

$template->assign_block_vars('topicrow', $topic_row);
Are you suggesting I make a single block_var and assign it that way? Would that even work since the code in viewforum has already been triggered?
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Adding a block_var array to another block_var array

Post by RMcGirr83 »

No I am suggesting that you do exactly what you are planning on doing all the topic row stuff is present in that event as far as I can tell.

https://wiki.phpbb.com/Event_List#core. ... _row_after
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Adding a block_var array to another block_var array

Post by 3Di »

You may try the event above of it core.viewforum_modify_topicrow in case.

@var array topic_row Template array with topic data
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53400
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding a block_var array to another block_var array

Post by Brf »

If you are trying to embed tagrow in topicrow, you would just use

Code: Select all

<!-- BEGIN tagrow -->
<div class="tag">{topicrow.tagrow.TOPIC_TAGS}</div>
<!-- END tagrow -->
See how "attachment" is done in "postrow" in viewtopic_body.html

Code: Select all

					<!-- BEGIN attachment -->
						<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
					<!-- END attachment -->
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Adding a block_var array to another block_var array

Post by Kailey »

Brf wrote: Wed Apr 04, 2018 8:21 pm If you are trying to embed tagrow in topicrow, you would just use

Code: Select all

<!-- BEGIN tagrow -->
<div class="tag">{topicrow.tagrow.TOPIC_TAGS}</div>
<!-- END tagrow -->
Any thoughts on the PHP portion for that?
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53400
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding a block_var array to another block_var array

Post by Brf »

Your PHP should be fine, assuming you are assigning it within the same loop that is creating the topicrow, just after the topicrow blockvars is assigned.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Adding a block_var array to another block_var array

Post by Kailey »

No errors, but they don't show either. Current code below.

PHP:

Code: Select all

public function viewforum_topic_row_after($event)
{
	$row = $event['row'];

	if ($this->topic_tags[$row['topic_id']])
	{
		$topic_tags = $this->topic_tags[$row['topic_id']];

		foreach ($topic_tags as $topic_tag)
		{
			$this->template->assign_block_vars('topicrow.tags', array(
				'TOPIC_TAG' => $topic_tag,
			));
		}

		$topic_row = array_merge($event['topic_row'], array(
			'HAS_TOPIC_TAGS' => true,
		));
	}
}
HTML:

Code: Select all

<!-- IF topicrow.HAS_TOPIC_TAGS -->
<br />
<!-- BEGIN tags -->
<div class="tag">{topicrow.tags.TOPIC_TAG}</div>
<!-- END tags -->
<!-- ENDIF -->
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Adding a block_var array to another block_var array

Post by martti »

kinerity wrote: Thu Apr 05, 2018 1:27 pm No errors, but they don't show either. Current code below.

PHP:

Code: Select all

public function viewforum_topic_row_after($event)
{
	$row = $event['row'];

	if ($this->topic_tags[$row['topic_id']])
	{
		$topic_tags = $this->topic_tags[$row['topic_id']];

		foreach ($topic_tags as $topic_tag)
		{
			$this->template->assign_block_vars('topicrow.tags', array(
				'TOPIC_TAG' => $topic_tag,
			));
		}

		$topic_row = array_merge($event['topic_row'], array(
			'HAS_TOPIC_TAGS' => true,
		));
	}
}
  • The $topic_row isn't assigned back to $event $event['topic_row'] = $topic_row;
  • But that wouldn't help either because $topic_row was already assigned to the template before core.viewforum_topic_row_after . If you want to assign HAS_TOPIC_TAGS to topicrow use core.viewforum_modify_topicrow
  • However, you could skip the topic_row.HAS_TOPIC_TAGS altogether in the template. Instead of
kinerity wrote: Thu Apr 05, 2018 1:27 pm HTML:

Code: Select all

<!-- IF topicrow.HAS_TOPIC_TAGS -->
<br />
<!-- BEGIN tags -->
<div class="tag">{topicrow.tags.TOPIC_TAG}</div>
<!-- END tags -->
<!-- ENDIF -->
you could use the Twig syntax:

Code: Select all

{%- if topicrow.tags -%}
	<br>
	{%- for t in topicrow.tags -%}
		<div class="tag">{{- t.TOPIC_TAG -}}</div>
	{%- endfor -%}
{%- endif -%}
No need for topic_row.HAS_TOPIC_TAGS for the conditional check.

Twig documentation on if statement:
https://twig.symfony.com/doc/2.x/tags/if.html
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Adding a block_var array to another block_var array

Post by martti »

I've been testing a bit:

In the phpBB old template syntax this conditional statement also works:

Code: Select all

<!-- IF topic.tags -->
CONTENT
 <!-- ENDIF -->
But the looping (in the event listener file topiclist_row_append.html ) doesn't work:

Code: Select all

<!-- BEGIN tags -->
<div class="tag">{topicrow.tags.TOPIC_TAG}</div>
<!-- END tags -->
But Twig syntax of my previous post works.
User avatar
ViolaF
I've Been Banned!
Posts: 1609
Joined: Tue Aug 14, 2012 11:52 pm

Re: Adding a block_var array to another block_var array

Post by ViolaF »

thx, martti :D

had a similar problem, ... now solved ..
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Adding a block_var array to another block_var array

Post by Kailey »

Haven't had a chance to test this yet, but thank you martti!
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Adding a block_var array to another block_var array

Post by mrgoldy »

Twig {% for %}

Just to add to martti, twig uses the |length filter.

Code: Select all

{% if some_loop|length %}
    The loop has items in it ..
{% endif %}
What also is possible is:

Code: Select all

{% for item in some_loop %}
    Do loop stuff
{% else %}
    Triggered when the loop is empty
{% endfor %}
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
Post Reply

Return to “Extension Writers Discussion”