Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Get page image url in onPageProcessed function

Started by Muut Archive 10 years ago · 5 replies · 436 views
10 years ago

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)

PHP
...
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.

10 years ago

ok i found not sure best way


$seofacebookimage = $this->grav['uri']->url .'/'. $value;

10 years ago

Where are you performing this logic? Also what does your page header look like?

10 years ago

i perform that on my template php when $uri = $route in order to get the right frontmatter

TXT
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"
}

10 years ago

ok, that helps thanks.

I would do something like this:

PHP
$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.

10 years ago

thanks @rhukster for putting me on the right track. I did like this

PHP
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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1366 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago