Get the Book title in custom twig template - drupal

I'd like to display the main Book title on all pages of a book, instead of only the current page's title. However, I can't find a way to print this Book title in my Twig template.
How can I get the main Book title and print it in a custom twig template?

In your hook_preprocess_node
function hook_preprocess_node(&$variables) {
// Get node object;
$node = $variables['node'];
$node_id = $node->id();
$entity = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);
if (isset($entity->book)) {
$book_id = $entity->book['p1'];
$book_parent_entity = \Drupal::entityTypeManager()->getStorage('node')->load($book_id);
$title = $book_parent_entity->getTitle();
$variables['book_title'] = $title;
}

Related

ACF get custom fields for a tag

I'm using ACF to create new custom fields to include more info in tags. Then I want to retrieve in the tag page like this:
$term_id = get_queried_object()->term_id;
$posts_ids = isset ($term_id) ? get_fields('articulos_destacados', 'taxonomy_'.$term_id, false) : null;
But nothing is retrieved. What am I doing wrong?

Plugin for custom field?

This plugin https://wordpress.org/plugins/no-external-links/
I’m using a custom field.
<?php $values = get_post_custom_values("imgurl"); echo $values[0]; ?>
The plugin does not mask this my custom field. How do I mask this link?
What code should I add to the function file? thanks
// You will have to add just a line in a theme code where you output custom field data.
// To add same preprocessing for data as for comment text, use
$metadata = apply_filters(‘comment_text’, $metadata);
// For example, if you use some kind of metadata, it should look like this:
$metadata = get_post_meta($id, ‘MetaTagName’, true); // get data from wordpress database
$metadata = apply_filters(‘comment_text’, $metadata); // add this line of code for preprocessing field value
echo $metadata; //output preprocessed field value
// Use “the_content” instead of “comment_text” if you want to use the same masking policy as for post text. https://www.ozgurbilgi.net

Drupal 8 page.html.twig per content type

Is there any way to make custom page.html.twig for custom content type for example
page--node--content-type.html.twig?
currently we get
page--node--%.html.twig...
Put below code to your theme_name.theme file
function theme_name_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$content_type = $node->bundle();
$suggestions[] = 'page__'.$content_type;
}
}
Then for 'custom' content type you can create 'page--custom.html.twig' file.

How to modify the body of a node via a module in Drupal 8

I created a module to highlight the code samples in the articles based on the highlight.js library. This library require code samples to be embed in <pre><code> ... </code></pre> tags. I don't want to add the <pre> tag when I write an article, because other highlighter libraries don't need it and I want to be able to switch library without modifying the articles. So I need to dynamically wrap <code> tags with <pre>. I tried to implement _node_view( and _node_view_alter( hooks without success.
My code look like
function highlight_node_view($node, $view_mode, $langcode) {
$node->content['body']['und'][0]['safe_value'] = "test";
$node->body['und'][0]['safe_value'] = "test";
$node->content['body']['und'][0]['value'] = "test";
$node->body['und'][0]['value'] = "test";
}
function highlight_node_view_alter($build, $node) {
$build["body"]["#items"]["0"]["value"] = "TEST";
$build["body"]["#items"]["0"]["safe_value"] = "TEST";
$build["body"]["#formater"]["0"]["#markup"] = "TEST";
}
If I print the content in my template with
<pre>
<?php
ob_start("minimal_htmlspecialchars_callback");
print_r($content);
ob_end_flush();
?>
</pre>
I can confirm that all the values are correctly replaced by "test" but a call to
<?php print render($content['body']); ?>
still render the original content instead of "test"
What is the proper way to alter the content of an article from a module ?
I have successfully altered body field data, you have to place code in your module file like bellow. BTW I'm using drupal 8.2
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_ENTITY_TYPE_view() for node entities.
*/
function module_name_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
$build['body'][0]['#text'] = 'test by ajay';
}
I finally found a solution in a previous stackoverflow question ( Modify how a node in drupal will look when viewed ) and the following code work fine.
function highlight_node_view($node, $view_mode, $langcode) {
$alteredBody = $node->body['und'][0]['safe_value'];
$alteredBody = str_replace("<code>", "<pre><code>", $alteredBody);
$alteredBody = str_replace("</code>", "</code></pre>", $alteredBody);
$node->content['body'] = array(
'#markup' => $node->body['und'][0]['safe_value'] = $alteredBody
);
}
But I'm still not sure to fully understand the rendering system. So if somebody have a better solution or a more detailed explanation, I'll appreciate it.

How to create custom pages (Drupal 6.x)

In the template.php file I inserted the code below:
I found a tutorial online that gives the code, but I'm confused on how to get it to work.
I copied the code below and inserted it into the template.php from the theme HTML5_base.
I duplicated the page.tpl.php file and created custom pages -- page-gallery.tpl.php and page-articles.tpl.php. I inserted some text to the files just see that I've navigated to the pages w/ the changes. It looks like Drupal is not recognizing gallery.tpl.php and page-articles.tpl.php.
In the template.php there are the following functions:
html5_base_preprocess_page()
html5_base_preprocess_node()
html5_base_preprocess_block()
In the tutorial it uses these functions:
phptemplate_preprocess_page()
phptemplate_preprocess_block()
phptemplate_preprocess_node()
function phptemplate_preprocess_page(&$vars)
{
//code block from the Drupal handbook
//the path module is required and must be activated
if(module_exists('path'))
{
//gets the "clean" URL of the current page
$alias = drupal_get_path_alias($_GET['q']);
$suggestions = array();
$template_filename = 'page';
foreach(explode('/', $alias) as $path_part)
{
$template_filename = $template_filename.'-'.$path_part;
$suggestions[] = $template_filename;
}
$vars['template_files'] = $suggestions;
}
}
function phptemplate_preprocess_node(&$vars)
{
//default template suggestions for all nodes
$vars['template_files'] = array();
$vars['template_files'][] = 'node';
//individual node being displayed
if($vars['page'])
{
$vars['template_files'][] = 'node-page';
$vars['template_files'][] = 'node-'.$vars['node']->type.'-page';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-page';
}
//multiple nodes being displayed on one page in either teaser
//or full view
else
{
//template suggestions for nodes in general
$vars['template_files'][] = 'node-'.$vars['node']->type;
$vars['template_files'][] = 'node-'.$vars['node']->nid;
//template suggestions for nodes in teaser view
//more granular control
if($vars['teaser'])
{
$vars['template_files'][] = 'node-'.$vars['node']->type.'-teaser';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-teaser';
}
}
}
function phptemplate_preprocess_block(&$vars)
{
//the "cleaned-up" block title to be used for suggestion file name
$subject = str_replace(" ", "-", strtolower($vars['block']->subject));
$vars['template_files'] = array('block', 'block-'.$vars['block']->delta, 'block-'.$subject);
}
Ah! That's a lot of pain to do this.
Why don't you try the Ctools and Panels modules.
Ctools has a lot of useful modules, the one you'd make good use of is called "Page Manager". It will allow you to create custom pages of your choice, and apply custom layouts using the Panels module.
Hope this simplifys things.

Resources