1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Add meta Property to Head from Fabrik Element?

Discussion in 'Community' started by talkinggoat, May 13, 2019.

  1. talkinggoat

    talkinggoat Member

    Level: Community
    I need to add a meta og:image property for facebook into the head of a page. Is it even possible to add elements to the head of a document from a Fabrik element or what is the best way? I can write it in Javascript, but it does no good, because Facebook scrapes the page before the JS has run.

    Currently, I am adding the image into a textarea element and assigning it an ID tag of fb_image, to make things a little more dynamic.

    Adding the meta in JS


    Code (Javascript):
    var fbImage = document.getElementById('fb_image').src;
    var createMeta = document.createElement('meta');
    createMeta.setAttribute('property','og:image');
    createMeta.content = fbImage;
    document.getElementsByTagName('head')[0].appendChild(createMeta);
     
  2. lousyfool

    lousyfool Active Member

    Level: Community
  3. startpoint

    startpoint Active Member

    Level: Community
    Through form php plugin may be work properly. I use it in custom template.
    PHP:
    $metatitle = $this->data['table_name___meta_title_raw'];
    $metadesc = $this->data['table_name____meta_description_raw'];
    $metakeywords = $this->data['table_name___meta_keywords_raw'];
    $image = $this->data['table_name___image'];
    $mydoc = JFactory::getDocument();
    $mydoc->setMetadata('title', $metatitle);
    $mydoc->setDescription($metadesc);
    $mydoc->setMetadata('keywords', $metakeywords);
    $mydoc->setMetadata('og:image',$image, 'property');
     
    Last edited: May 13, 2019
    talkinggoat and cheesegrits like this.
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yup, you should be able to do that in a form PHP plugin, on something like onLoad.

    -- hugh
     
    talkinggoat likes this.
  5. talkinggoat

    talkinggoat Member

    Level: Community
    Thank you. I set this to OnLoad in the Form's plugin section as PHP and it works wonderfully.


    PHP:
    $metaImage = $formModel->data["job_types___volunteer_application_image"]; //Gets the HTML for the image.
    $metaDesc = $formModel->data["job_types___position_description"]; //Gets description.
    $metaTitle = $formModel->data["job_types___position_title"];

    if (isset($metaImage)){
      $doc = new DOMDocument(); //Parses the HTML.
      $doc->loadHTML($metaImage); //Inserts the HTML from the image.
      $xpath = new DOMXPath($doc);
      $src = $xpath->evaluate("string(//img/@src)"); //Strips out the src property of the image.
      $baseURL= JURI::base(); //Gets the base URL
        $src = $baseURL.$src; //Combines the base with the image URL.
      $mydoc = JFactory::getDocument();
      $mydoc->setMetadata('og:image',$src, 'property');
    }

    if (isset($metaDesc)){
      $mydoc = JFactory::getDocument();
      $mydoc->setDescription($metaDesc);
    }

    if (isset($metaTitle)){
      $mydoc = JFactory::getDocument();
      $mydoc->setTitle("Volunteer Wanted: ".$metaTitle);
    }
     
     
  6. sunnyjey

    sunnyjey Active Member

    Level: Community
    Thank you for sharing your code. I was looking for it since long time.

    I have some listing which has just single Image and few have multiple images. How to modify the code to check whether if Image element contains single image or multiple images. If it contain multiple images, how to set it to pick up the First image ?
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    If it has multiple images, that's probably an array ...

    Code (Text):

    if (is_array($formModel->data["job_types___volunteer_application_image"]) {
       // get first item from array
       $metaImage = reset($formModel->data["job_types___volunteer_application_image"]);
    }
    else {
       $metaImage = $formModel->data["job_types___volunteer_application_image"];
    }
     
    However, note that if you are using AJAX uploads, I have a feeling that $metaImage may itself wind up being an array of data (crop details, etc), rather than a simple path string. SO if the above doesn't work, try var_dump()'ing $metaImage and see what it is.

    -- hugh
     
    sunnyjey and talkinggoat like this.
  8. sunnyjey

    sunnyjey Active Member

    Level: Community
    Can we insert dynamic meta Property to the Fabrik List through Php List plugin, if yes how ?
     
  9. lousyfool

    lousyfool Active Member

    Level: Community
    sunnyjey likes this.
  10. sunnyjey

    sunnyjey Active Member

    Level: Community
    Code (Text):
    $data = $model->getData();
    $city = $d->filters['property___city']->element;// Gets the Name of City from List filter.

    if (isset($metaDesc)){
      $mydoc = JFactory::getDocument();
      $mydoc->setDescription("Check out best property from: ".$city);
    }

    if (isset($metaTitle)){
      $mydoc = JFactory::getDocument();
      $mydoc->setTitle("Top Properties from: ".$city);
    }
    I'm not sure, is this right code to have Meta data in Php Event List plugin 'onFiltersGot' ?

    NB: Apologies for using this Thread instead of creating a new one. I feel, it makes more sense to have one Thread for Meta Data for Detail view and List are related. It would be easier to find others who are looking for same.
     
    lcollong likes this.
  11. sunnyjey

    sunnyjey Active Member

    Level: Community
    Unfortunately, the above code of Meta for List is not working!

    Would appreciate any help in coding the correct php syntax to obtain Meta information for the List.
     
  12. startpoint

    startpoint Active Member

    Level: Community
    Metadata in list view:
    Place metadata code in Php Event List plugin ->event->"onLoadData".
    Metadata in list form/details view:
    Place metadata code in Php Form plugin ->process script->"Before the form is loaded(OnLoad)".
     
  13. sunnyjey

    sunnyjey Active Member

    Level: Community
    I tried, but no success. Maybe my following Metadata code for Event Php List plugin is wrong.

    Code (Text):
    $data = $model->getData();
    $city = $d->filters['property___city']->element;// Gets the Name of City from List filter.

    if (isset($metaDesc)){
      $mydoc = JFactory::getDocument();
      $mydoc->setDescription("Check out best property from: ".$city);
    }

    if (isset($metaTitle)){
      $mydoc = JFactory::getDocument();
      $mydoc->setTitle("Top Properties from: ".$city);
    }
     
     
  14. startpoint

    startpoint Active Member

    Level: Community
    I don't know is this code is a part of another code.
    If not then you not have variables $metaDesc and $metaTitle.
    Try to set metadata directly only with:
    PHP:
    $mydoc = JFactory::getDocument();
    $mydoc->setDescription("Check out best property from: ");
     
  15. sunnyjey

    sunnyjey Active Member

    Level: Community
    This is working, BUT it is setting MetaData to both List and Detail view. I want to have different MetaData to ListView and Detail View.


    PHP:
    $mydoc = JFactory::getDocument();
    $mydoc->setTitle("Check out best property from: ");
    This is also working, BUT strangely it is setting MetaTitle to Detail view and NOT LIST view.
     
  16. startpoint

    startpoint Active Member

    Level: Community
    Set list metadata:
    PHP:
    $mydoc = JFactory::getDocument();
    $mydoc->setDescription("Check out best property from: LIST");
    $mydoc->setMetadata('title', 'mymetatitle');
    //$mydoc->setTitle("Check out best property from: "); not working in list view
     
    Try to clear your cache.
    I don't have your problem
     

Share This Page