Drupal 8 passing current nid to block template - symfony

I have created a custom module that uses a custom block to display a list of nodes (I can't use D8 views in this particular case).
Inside the block twig template I need to filter this list on the current nid (node id of the current page) and I'm passing the value to twig in this way:
$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->nid->value;
return array(
'currentnid' => $nid,
);
Everything is working fine, but changing page the nid passed to the block template is still the same. I think this is related to template caching: by clearing the cache and reloading the page I get the correct value for the current page, but moving to another page I'm still getting the same nid!
Is there something I can do to prevent nid value to be cached?
I think that the best solution would be to get the current nid value directly inside the template, but I'm inside the custom block and I don't know how to get it. Any idea? I have already checked the kint() output and the current page nid is not present at the block template level.

<?php
namespace Drupal;
$node_v1 = \Drupal::request()->attributes->get('node');
$node_v2 = \Drupal::routeMatch()->getParameter('node')
?>

in your build array try using cache tags with your current nid
return array(
'currentnid' => $nid,
'#cache' => [
'tags' => ['node:'.$nid],
],
);

Related

How can I add a paragraph (as default) in the node form by using hook_form_alter in Drupal 8?

I am trying to add a bunch of different empty paragraphs of different types, to a entity reference revisions field, everytime a node of a certain content type is created.
I DON'T want to use the contrib module "default paragraphs" for this, because I need to use a certain form widget here, and default paragraphs is also achieved by a widget.
What I tried so far:
function myModule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
$paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
'type' => 'tab_features'
]);
$paragraph->save();
$form['field_tabs']['widget'][0]['target_id']=$paragraph->id();
$form['field_tabs']['widget'][0]['target_revision_id']=$paragraph->getRevisionId();
return $form;
}
$field_tabs is my entity reference revisions field.
'tab_features' is the paragraphs type I want to add.
I guess there should be a method that can be used in the form or form widget to add a paragraph to the form, like someone already clicked the button to add it. I want to avoid to actually trigger this via Javascript if possible. Anybody knows how to do this in form_alter?
In a project I'm working on, we have done something like this:
//get the body field
$field = $entity->get('field_em_p_body');
$paragraph = Paragraph::create([
'type' => 'em_section', // Paragraph type.
]);
$paragraph->isNew();
$paragraph->set('YOUR_FIELD', 'SOMETHING');
$field->appendItem($paragraph);

Changing Type from Content To Global or VDC Drupal

I'm using Drupal 7 with the view_database_connector module. I'm currently working with a view that consists of a table displaying database information. My goal is to have a field with action buttons corresponding to each row, such as delete.
I am not allowed to use global php as a field.
I've attempted to make a custom module following this. I can currently use this module on content tables, however, when I try to use it on my view_database_connector table, I'm unable to add it as a field, since it's not apart of the same group.
Here's where I set up the information for making the action:
function mymodule_views_data_alter(&$data) {
// debug($data['node']);
$data['node']['actions'] = array(
'title' => t('Actions'),
'help' => t('Clickable links to actions a user may perform on a Node.'),
'field' => array(
'handler' => 'mymodule_views_handler_field_actions',
'group' => 'Content',
'click sortable' => FALSE,
),
);
}
I've tried deleting Content, changing it to global, and changing it to the VDC type, but none of that changes it into Global or VDC.
Alternatively, if there's an easier way just to hook into a field that has a button to run my code which will download a file, that could circumvent this issue.
You don't have to create this feature on your own - it already exists in views. There is a field type with actions as delete.
Other option is to use option called "rewrite value of this field" (or similar). With it you can make your field from views interface, and totally rewrite it's output. You can use token and some of them for sure contains node id, so you can use it to generate link to node deleting/editing/viewing pages.

Is it possible to do a loop inside the option page of plugins on wordpress?

I've encounter this issue during the develop of my plugin.
I've to loop through all the pages of the website and print the titles and the IDs of them.
When I do the simply loop it just don't print anything.
Is there a way for do a loop inside options of my plugin?
Found the solution.
I don't know if there's some kind of block inside the plugin options page but we can loop through every custom post type with a little portion of code:
<?php
//Search only for custom post type which is public.
$args = array('public'=>true);
//Get all custom post type name
$allMyCustomPostTypes = get_post_types($args);
foreach ($allMyCustomPostTypes as $myCustomPostType) {
//Create a filter for get_posts with every custom post type
$filter = array('numberposts' => 5, 'post_type' => $myCustomPostType);
//Pass the value to *get_posts*
$theposts = get_posts($filter);
foreach ($theposts as $thepost) {
//Easy print the name of the current post of the current custom post type
echo $thepost->post_title;
};
};
?>
Here we are, hope this helps somebody.

Treat the node nids as a field (for display only in a content type) in drupal 7

I need to use the nid of a node as field in a content type: i need to choose where to print it (put it before some fields but after others) and format it as i wish. the only thing i could think about is create a "fake" custom field with no widget to insert it buth with a theme formatter to display it but it seems to me that this is a little to complicated. How should i do it?
If I understand correctly, you just want to expose data to the node view. Could it be as easy as using hook_node_view() from a module?
With that, you can set a 'fake' field to be sent out to the content array of the node, which you can access in the node template.
From drupal.org:
<?php
function hook_node_view($node, $view_mode, $langcode) {
$node->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
}
?>

Drupal 7: calling custom content type field into page tpl

I'm working on a Drupal 7 website. I need custom layout for some pages. so I created page--customContentTypeName.tpl.php file and it addresses perfectly.
The problem is, I need to display some fields in page tpl. The code below works fine in node tpl, but page tpl :/
<?php print $content['field_images']['#items']['0']['filename']; ?>" />
How can I call custom fields into page tpl?
Appreciate helps!! thanks a lot!!
** SORTED **
with custom field editing... here is the tutorial video: http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54
For page.tpl.php
if you access the node directly you can use $node variable
$node['field_images']['und'][0]['filename']
else use $page variable.
$page['content']['system_main']['nodes'][1]['field_images']['#items'][0]['filename'];
but remember in a page variable you might have more than one node.
The structure changed in 7, the field is keyed by language first ("und" by default which means "undefined"), then you can follow this example:
// Array of Image values
$images = $node->field_images['und'];
//If you don't know the language, the value is stored in:
$node->language
// First image
$image = $images[0];
// If you need informations about the file itself (e.g. image resolution):
image_get_info( $image["filename"] );
// If you want to access the image, use the URI instead of the filename !
$public_filename = file_create_url( $image["uri"] );
// Either output the IMG tag directly,
$html = '<img src="'.$public_filename.'"/>';
// either use the built-in theme function.
$html = theme(
"image",
array(
"path" => $public_filename,
"title" => $image["title"]
)
);
Note the usage of the uri instead of the filename for embedding the image in a page because the File API in Drupal 7 is more abstracted (to make it easier to integrate with CDN services).
there are 2 useful modules in drupal for theme developers:
Devel and Theme_developer
Devel module provides a function called dsm() .
using dsm you can recognize that how elements are stored in different objects.
like nodes or ...
for example you can use this statement : dsm($node)
the structure of any nodes in the page will show up in the message box.
you can type the statements in your codes.

Resources