Forum

Thread tagged as: Question, Runway

Passing Author Collection Relationship fields into Page Attributes for Articles

I have two collections one for Blog and one for Authors. In my post's meta data I want to show the article:author meta but as it is relationship data it wont pull through like it does with the blog pots's collection data. Is it possible to do? I'm using the code below:

attributes file contains

<meta property="article:author" content="<perch:pages id="og_author" type="hidden" />" />

post.php contains

<?php
# get the post
$post = perch_collection('Blog', [
      'filter'        => 'titleSlug',
      'match'         => 'eq',
      'value'         => perch_get('s'),
      'skip-template' => 'true',
      'return-html'   => 'true',
    ]);

# set up the variables
$title       = $post['0']['heading'];
$description = strip_tags($post['0']['desc']);
$keywords = strip_tags($post['0']['keywords']);
$noindex = strip_tags($post['0']['noindex']);
$nofollow = strip_tags($post['0']['nofollow']);
$nosnippet = strip_tags($post['0']['nosnippet']);
$authorfirst = strip_tags($post['0']['author_first_name']);
$authorlast = strip_tags($post['0']['author_last_name']);
if (isset($post[0]['postImageSocial'])) {
    $socialImage = $post[0]['postImageSocial'];
} else {
    $socialImage = '';
}

# use the variables in the array value 
perch_page_attributes_extend(array(
    'description'    => $description,
    'keywords'       => $keywords,
    'noindex'        => $noindex,
    'nofollow'       => $nofollow,
    'nosnippet'      => $nosnippet,
    'sitename'       => 'CPI',
    'og_description' => $description,
    'og_title'       => $title,
    'og_author'      => $authorfirst . ' ' . $authorlast,
    'og_type'        => 'article',
    'og_image'       => $socialImage,
    'sharing_image'  => $socialImage,
));
?>
Dan Lee

Dan Lee 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Instead of $post['0'], try $post[0]

Dan Lee

Dan Lee 1 points

Hi Drew, I'm afraid that didn't work.

$authorfirst = $post[0]['author_first_name'];
$authorlast = $post[0]['author_last_name'];
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does print_r($post) give you?

Dan Lee

Dan Lee 1 points

It outputs the following on the front end:

Array ( [0] => Array ( [_id] => 1 [heading] => Fish waste out of water... and swimming to your home [_title] => Fish waste out of water... and swimming to your home [titleSlug] => fish-waste-out-of-water-and-swimming-to-your-home [date] => 2015-09-29 [desc] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [keywords] => CPI, centre for process innovation [noindex] => noindex [nofollow] => nofollow [nosnippet] => nosnippet [postImageSocial] => /dynamic/login/resources/salmon01.jpg [postImageHead] => /dynamic/login/resources/salmon-school.jpg [postImageCard] => /dynamic/login/resources/ecoli.jpg [author] => Array ( [0] => 2 ) [_blocks] => Array ( [0] => Array ( [headingBlock] => [headingBlockNo] => [textBlock] => Array ( [_flang] => markdown [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante.

) [_block_type] => text [_block_id] => nvg5bu [_block_index] => 0 ) [1] => Array ( [image] => Array ( [0] => Array ( [imgCaption] => In Norway alone, over a million tonnes of fish waste is generated every year. [weight] => left [postImageOne] => Array ( [assetID] => 3 [title] => Salmon03 [_default] => /dynamic/login/resources/salmon03.jpg [bucket] => default [path] => salmon03.jpg [size] => 1890030 [w] => 1285 [h] => 1920 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 100 [h] => 150 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => salmon03-thumb@2x.jpg [size] => 22829 [mime] => [assetID] => 4 ) ) ) [postImageTwo] => Array ( [assetID] => 1 [title] => Salmon01 [_default] => /dynamic/login/resources/salmon01.jpg [bucket] => default [path] => salmon01.jpg [size] => 2569833 [w] => 1920 [h] => 1285 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 150 [h] => 100 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => salmon01-thumb@2x.jpg [size] => 24562 [mime] => [assetID] => 2 ) ) ) [postImageThree] => ) ) [_block_type] => image [_block_id] => nvhw09 [_block_index] => 1 ) [2] => Array ( [headingBlock] => [headingBlockNo] => [textBlock] => Array ( [_flang] => markdown [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante.

) [_block_type] => text [_block_id] => nvr8m5 [_block_index] => 2 ) [3] => Array ( [quoteBlock] => Array ( [_flang] => plain [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. ) [_block_type] => quote [_block_id] => nvjg8i [_block_index] => 3 ) [4] => Array ( [headingBlock] => [headingBlockNo] => [textBlock] => Array ( [_flang] => markdown [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante.

) [_block_type] => text [_block_id] => nvr76j [_block_index] => 4 ) [5] => Array ( [image] => Array ( [0] => Array ( [imgCaption] => Amino acids, bioplastic and biofuels can be made using enzymes taken from fish cells. [weight] => right [postImageOne] => Array ( [assetID] => 11 [title] => Ecoli [_default] => /dynamic/login/resources/ecoli.jpg [bucket] => default [path] => ecoli.jpg [size] => 1461427 [w] => 1285 [h] => 1920 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 100 [h] => 150 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => ecoli-thumb@2x.jpg [size] => 17222 [mime] => [assetID] => 12 ) ) ) [postImageTwo] => Array ( [assetID] => 9 [title] => 474598139 [_default] => /dynamic/login/resources/salmon-school.jpg [bucket] => default [path] => salmon-school.jpg [size] => 3113293 [w] => 3000 [h] => 2000 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 150 [h] => 100 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => salmon-school-thumb@2x.jpg [size] => 24052 [mime] => [assetID] => 10 ) ) ) [postImageThree] => ) ) [_block_type] => image [_block_id] => nvr85z [_block_index] => 5 ) [6] => Array ( [headingBlock] => [headingBlockNo] => [textBlock] => Array ( [_flang] => markdown [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante.

) [_block_type] => text [_block_id] => nvr777 [_block_index] => 6 ) [7] => Array ( [image] => Array ( [0] => Array ( [imgCaption] => Enzymes are used in washing detergents, food processing and cosmetics [weight] => left [postImageOne] => Array ( [assetID] => 7 [title] => 76762340 [_default] => /dynamic/login/resources/facial-cream.jpg [bucket] => default [path] => facial-cream.jpg [size] => 4264418 [w] => 5127 [h] => 3409 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 150 [h] => 99 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => facial-cream-thumb@2x.jpg [size] => 9703 [mime] => [assetID] => 8 ) ) ) [postImageTwo] => [postImageThree] => ) ) [_block_type] => image [_block_id] => nvr7mo [_block_index] => 7 ) [8] => Array ( [headingBlock] => This is a heading [headingBlockNo] => 1 [textBlock] => Array ( [_flang] => markdown [raw] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante. [processed] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet metus lorem. Morbi rutrum purus ac gravida lobortis. Curabitur eu felis aliquam, maximus dolor ut, aliquam ante.

) [_block_type] => text [_block_id] => nvr7w5 [_block_index] => 8 ) ) [_sortvalue] => 1000 ) [html] =>
Drew McLellan

Drew McLellan 2638 points
Perch Support

So no author_first_name and no author_last_name. Where would you expect those to be?

Dan Lee

Dan Lee 1 points

They are on the post.php page wrapped in the relationship template tags. It is correctly outputting the information on the front end using the following:

<perch:related id="author" collection="Authors" label="Author">
    <perch:content id="author_first_name" /> <perch:content id="author_last_name" />
</perch:related>

The only reference to author in the print out is the following - which is where the first and last name is represented in the front end template.

[author] => Array ( [0] => 2 )
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ah, ok, I've got you. So those are the IDs of the related items in the Authors collection.

perch_collection('Authors', ['_id'=>2]);

might get you there.

Dan Lee

Dan Lee 1 points

Thanks, where should I be using that line?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's an example - not a literal instruction :)

if (isset($post[0]['author']) && is_array($post[0]['author'])) {
    foreach($post[0]['author'] as $authorID) {
        $author = perch_collection('Authors', [
             '_id'=>$authorID,
             'skip-template' => true,
        ]);
        // do whatever you will with the $author.
    }
}
Dan Lee

Dan Lee 1 points

Thanks Drew - a great help once again!

Full solution for anyone else trying to do this:

if (isset($post[0]['author']) && is_array($post[0]['author'])) {
    foreach($post[0]['author'] as $authorID) {
        $author = perch_collection('Authors', [
             '_id'=>$authorID,
             'skip-template' => true,
        ]);
        // do whatever you will with the $author.
        $authorfirst = $author['0']['author_first_name'];
        $authorlast = $author['0']['author_last_name'];
    }
}