What is the Drupal 8 equivalent of Drupal 7's "drupal_add_html_head" within a module?

Anders8

I'm creating some custom modules for Drupal 8. They include some header modification for better Facebook integration. Here is what it looked like in Drupal 7 (SEVEN):

    $element1 = array
        (
        '#tag' => 'meta',
        '#attributes' => array
            ( 
            'property' => 'og:title',
            'content' => " Profile: " . $record->NameFirst . " " . $record->NameLast,
            ),
        );
    drupal_add_html_head($element1, 'og_title');

But this drupal_add_html_head function is long long gone in Drupal 8. And I'm quite lost as to where to START attacking this. Maybe it's "Headerbag"? There's a Headerbag::add. Maybe it's in the module's return variable, possibly adding another element somewhere here:

return array(
  '#markup' => t($pageContent),
);

Maybe HtmlResponseAttachmentsProcessor::setHeaders? HeaderBag::set? Session::setRequestHeader? PoStreamWriter::setHeader? PoMetadataInterface::setHeader?

Unfortunately, I can find pretty much no examples of how these are used. And I'm sure everyone here is familiar with the annoyance of having code that does work in previous versions that morphs into "doesn't work with no solution" in new code.

Frank Drebin

You can use the your_module_page_attachments hook. For example if you want to adjust the og:image tag you could do the following:

function your_module_page_attachments(array &$page) {
    $ogImage = array(
        '#tag' => 'meta',
        '#attributes' => array(
            'property' => 'og:image',
            'content' => url to your image,
        ),
    );

    $page['#attached']['html_head'][] = [$ogImage, 'ogImage'];
}

Hook information: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_page_attachments/8.2.x

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Drupal 8 Creating a module

From Dev

Difference between Drupal 7 And Drupal 8 for beginners

From Dev

unable to create drupal 7 module

From Dev

Order by content in Drupal 7 module

From Dev

issue in webform module of drupal 7

From Dev

Drupal 7 examples module not working

From Dev

Best ecommerce module for Drupal 7

From Dev

ckeditor in Drupal 8 custom module

From Dev

Drupal 8 Module Development Dependencies

From Dev

Drupal 8 Custom Module Libraries

From Dev

Drupal 8 - Block module as a webservice

From Dev

Is there the equivalent of the WP plugin Types for Drupal 7?

From Dev

In the drupal 7 rules module's execute custom php action how can I find out what the $site variable specifically contains?

From Dev

How to set permission in custom drupal module in Drupal7

From Dev

Where are the css(s) in drupal 7?

From Dev

Wiki-like Module in Drupal 7

From Dev

emails in drupal 7 using mime mail module

From Dev

How to work with the Entity Registration module in Drupal 7?

From Dev

Delete module from database in drupal7

From Dev

Placing a Module to a Certain Block in Drupal 7

From Dev

Drupal 7 webform module interpretation of the webform

From Dev

Drupal 8 custom module add php classes

From Dev

"Hierarchical Select" alternate module for Drupal 8

From Dev

Deleting Content On Module Uninstall in Drupal 8

From Dev

Drupal 7: How do I retrieve a node's Content Type from within a component's prepocess function?

From Dev

How to concretely display any kind of content within a drupal module

From Dev

What's the easiest way to install Drupal locally?

From Dev

What's the easiest way to install Drupal locally?

From Dev

Drupal 7: Taxonomies or Relations? what's a good why to strcuture hierarchial content

Related Related

  1. 1

    Drupal 8 Creating a module

  2. 2

    Difference between Drupal 7 And Drupal 8 for beginners

  3. 3

    unable to create drupal 7 module

  4. 4

    Order by content in Drupal 7 module

  5. 5

    issue in webform module of drupal 7

  6. 6

    Drupal 7 examples module not working

  7. 7

    Best ecommerce module for Drupal 7

  8. 8

    ckeditor in Drupal 8 custom module

  9. 9

    Drupal 8 Module Development Dependencies

  10. 10

    Drupal 8 Custom Module Libraries

  11. 11

    Drupal 8 - Block module as a webservice

  12. 12

    Is there the equivalent of the WP plugin Types for Drupal 7?

  13. 13

    In the drupal 7 rules module's execute custom php action how can I find out what the $site variable specifically contains?

  14. 14

    How to set permission in custom drupal module in Drupal7

  15. 15

    Where are the css(s) in drupal 7?

  16. 16

    Wiki-like Module in Drupal 7

  17. 17

    emails in drupal 7 using mime mail module

  18. 18

    How to work with the Entity Registration module in Drupal 7?

  19. 19

    Delete module from database in drupal7

  20. 20

    Placing a Module to a Certain Block in Drupal 7

  21. 21

    Drupal 7 webform module interpretation of the webform

  22. 22

    Drupal 8 custom module add php classes

  23. 23

    "Hierarchical Select" alternate module for Drupal 8

  24. 24

    Deleting Content On Module Uninstall in Drupal 8

  25. 25

    Drupal 7: How do I retrieve a node's Content Type from within a component's prepocess function?

  26. 26

    How to concretely display any kind of content within a drupal module

  27. 27

    What's the easiest way to install Drupal locally?

  28. 28

    What's the easiest way to install Drupal locally?

  29. 29

    Drupal 7: Taxonomies or Relations? what's a good why to strcuture hierarchial content

HotTag

Archive