Muut
August 2, 2016, 3:28pm
1
Hi everyone,
I’m stuck with that function, i’m triyng to get the img url from a page frontmatter image name (save with pagemediaselect)
...
foreach($header as $key => $value) {
$seofacebookimage_key = 'seofacebookimg';
if (array_key_exists('seofacebookimg', $header) ) {
if ( $key == $seofacebookimage_key && ! empty ($value) ) {
$seofacebookimage = $value;
$property = 'og:image';
$metadata[$property] = [ 'property' => $property, 'content' => $seofacebookimage ];
}
} else {
...
}
}
...
of course $value
return only myimage.jpg
because it’s in the frontmatter, how to get the full url ? Maybe add the page url in the frontmatter and then concatenate the 2 vars ?
thanks for any tips.
Muut
August 2, 2016, 4:42pm
2
ok i found not sure best way
$seofacebookimage = $this->grav[‘uri’]->url .’/’. $value;
Muut
August 2, 2016, 4:52pm
3
Where are you performing this logic? Also what does your page header look like?
Muut
August 2, 2016, 5:17pm
4
i perform that on my template php when $uri = $route
in order to get the right frontmatter
public function onPageProcessed(Event $e)
{
}
my page header :
{#233 ▼
+“title”: “Contact”
+“metadata”: array:1 [ ]
+“routable”: true
+“cache_enable”: true
+“visible”: true
+“seofacebookimg”: “normes.png”
+“content”: array:2 [ ]
+“form”: array:5 [ ]
+“onpage_menu”: true
+“body_class”: “index”
+“header_class”: “alt”
}
Muut
August 2, 2016, 5:48pm
5
ok, that helps thanks.
I would do something like this:
$page = $e['page'];
$header = $page->header():
$metadata = $page->header()->metadata;
if (isset($header->seofacebookimg)) {
$image = $header->seofacebookimg;
$found_image = $page->media[$image];
if ($found_image) {
$property = 'og:image';
$metadata[$property] = [ 'property' => $property, 'content' => $found_image->url(true) ];
}
}
Completely untested, but something along those lines.
Muut
August 3, 2016, 8:17am
6
thanks @rhukster for putting me on the right track. I did like this
if (isset($header->seofacebookimg)) {
$image = $header->seofacebookimg;
$found_image = $page->media();
$listmedia = $found_image->images();
$imgpath = $listmedia[$image]->url(true);
$base = $this->grav['uri']->base();
$fullpath = $base . $imgpath;
if ($found_image) {
$property = 'og:image';
$metadata[$property] = [ 'property' => $property, 'content' => $fullpath ];
}
}
I did like this because i need the full path image $imgpath = $listmedia[$image]->url(true);
give me the url without base
so i add $base = $this->grav['uri']->base();
and i concatenate both