Adding a block_var array to another block_var array

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

Adding a block_var array to another block_var array

Post by kinerity » Wed Apr 04, 2018 6:40 pm

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 Truscott - Community Team

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20784
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Adding a block_var array to another block_var array

Post by RMcGirr83 » Wed Apr 04, 2018 7:15 pm

core.viewforum_topic_row_after :?:
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1575
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Adding a block_var array to another block_var array

Post by kinerity » Wed Apr 04, 2018 7:54 pm

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 Truscott - Community Team

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20784
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Adding a block_var array to another block_var array

Post by RMcGirr83 » Wed Apr 04, 2018 7:59 pm

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
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
3Di
Registered User
Posts: 12453
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Adding a block_var array to another block_var array

Post by 3Di » Wed Apr 04, 2018 8:01 pm

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

@var array topic_row Template array with topic data
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51044
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 » 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 -->
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
kinerity
Community Team Member
Community Team Member
Posts: 1575
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Adding a block_var array to another block_var array

Post by kinerity » Thu Apr 05, 2018 2:47 am

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 Truscott - Community Team

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51044
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 » Thu Apr 05, 2018 11:55 am

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
kinerity
Community Team Member
Community Team Member
Posts: 1575
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Adding a block_var array to another block_var array

Post by kinerity » 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,
		));
	}
}
HTML:

Code: Select all

<!-- IF topicrow.HAS_TOPIC_TAGS -->
<br />
<!-- BEGIN tags -->
<div class="tag">{topicrow.tags.TOPIC_TAG}</div>
<!-- END tags -->
<!-- ENDIF -->
Kailey Truscott - Community Team

User avatar
martti
Registered User
Posts: 502
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Adding a block_var array to another block_var array

Post by martti » Sat Apr 07, 2018 6:51 am

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: 502
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Adding a block_var array to another block_var array

Post by martti » Sat Apr 07, 2018 8:21 am

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
Registered User
Posts: 1410
Joined: Tue Aug 14, 2012 11:52 pm

Re: Adding a block_var array to another block_var array

Post by ViolaF » Sun Apr 08, 2018 1:55 pm

thx, martti :D

had a similar problem, ... now solved ..

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1575
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Adding a block_var array to another block_var array

Post by kinerity » Sun Apr 08, 2018 4:31 pm

Haven't had a chance to test this yet, but thank you martti!
Kailey Truscott - Community Team

User avatar
posey
Registered User
Posts: 706
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Adding a block_var array to another block_var array

Post by posey » Sun Apr 08, 2018 8:53 pm

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 %}
''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Bing [Bot] and 5 guests