I have a task of making some content of movies in admin panel in drupal
https://imgur.com/a/5eJp9ZQ
and then after that to use a custom module and controller to show those movies onto my custom twig.
I am unable to find a solution or hint how to do that, maybe i even did find something but because of my small knowledge of drupal i dont know what i have to do in order to achieve that.
I created a custom module, controller , routing and everything while following the hello world part of drupal documentation but after that i just frooze and i am unable to continue further
Can someone assist me on this task, will be greatly appreciated because i have been stuck on it for 2 days now
Drupal's way of getting content is by using views. You should create a view (Structure -> Views -> Add view from admin menu) to get that data. It's something like visual interface for creating database queries, and it's not hard as it might look at first sight.
Then, there are few way how to use that view.
View can create a page, or a block, so content will appear on the page
You can execute view from your controller and collect data view returned and pass it to twig template to render the data.
You could embed view directly from twig, so no need for any kind of coding inside page controller.
Option 1 with "page view" (it's display type is "page") is probably easiest one, but doesn't fulfill request of custom controller.
With option 2 you could use: views_get_view_result() call to get results:
https://api.drupal.org/api/drupal/core%21modules%21views%21views.module/function/views_get_view_result/8.2.x
It should look something like:
$view = Views::getView('view_machine_name');
$view->execute();
foreach ($view->result as $row) { // iterating trough rows of results
// Do something with $row
}
This option matches the best your request.
For option 3 you would have to Twig Tweak module to embed view from twig:
https://www.drupal.org/docs/8/modules/twig-tweak/twig-tweak-and-views
Beside that you could of course create custom database query, but that's not so "drupalish" way and queries can be pretty complicated since drupal's database structure is not the simplest one and you could get lost.
Related
How can I show multi views on single page in Drupal?
I need to show two results, (maybe you can think 2 tables, but actually 2 views) on one page.
Anyone please help me.
You can use the panels module. That will allow you to put views in different types of layout on a page. And take arguments as required.
You can do it by creating a page with your first view. For the second view you have to create a block and add it to that page.
btw: please use https://drupal.stackexchange.com/ for drupal specific questions...
As laebs suggested one way is to create block view and to add it to some region when your page is displayed.
Other way would be embedding the view from template file:
https://api.drupal.org/api/views/views.module/function/views_embed_view/7
With this function you can specify which view and which display you want to embed. You can also pass the parameters this way.
And notice that you have "embed" display type (it may be hidden by default - check view options). If you use it you'll also get those nice "edit view" links when embedding view this way.
There are two ways that I know of without using other modules:
Use the Views Header/Footer area to load another view. Just click "Add" in the Head/Footer area of the Views UI and you should see "Global: View area".
Create a page template and insert the views that you'd like to have on the page within the template using some Drupal API php:
<?php
$view = views_get_view('view_machine_name');
print $view->execute_display('default', $args);
?>
I'm using a webform as a block on all the pages from a specific content type. The form is the same in all, but on the list of submissions I want to know which page the user was seeing. I thought a hidden field, and a default value of %title would do it, but it's not the case. I tried a bunch of other token values, and a lot of them do work, but none of them gives me an identifier of the current node.
I think the reason it's not working is because the node element of the webform tokens refers to the node of the webform itself, not the current node page. Why webform doesn't use the in-built token system I have no idea, seems like a missed opportunity.
Looking at the webform module I think the easiest way to get this done is to write a quick custom module defining a single table (two columns, nid and url) and then implement hook_webform_submission_insert() to save the current $_SERVER['REQUEST_URI'] for all webforms. You can then use hook_webform_submission_load() and hook_webform_submission_render_alter() respectively to re-attach the URL data to the submission and display it to the page.
You can find all of those hooks in the webform_hooks.php file included in the module, they're pretty well documented.
I have created a page view using field style to display a list of teasers as I want. fields configuration in views allows us to link the field to it's node. but what if we have created view for node detail page and want field to link to it's relevant view.
I had the same question, and after a bit of research this is what I've concluded:
The views module isn't intended to replace a default node view. I say this because of the level of difficulty involved in doing this and the lack of information on how to accomplish this. Ryan Weal has posted a way to accomplish this by editing your node template that doesn't look too difficult to accomplish, and here is the link.
However, it seems that a more popular solution, especially if you are like me and don't like to get into editing theme files. You can use the Display Suite module to effectively reformat the default node content as you would like.
I'm not sure if I understand you properly, but it sounds like you are wanting to display a view in a page view of a node?
In order to do this, you could:
create a small module, using hook_nodeapi() or one of the D7 replacements for this function in order to insert the output of the embedded view into this page's content, by conditionally adding a $content element when the node is of the appropriate id
or (easier, but requires allowing input type PHP) embed the view right on the page.
http://thedrupalblog.com/embedding-view-drupal-6-using-views-embed-view for information on embedding views
When I create a content type, I want to show all the posts of this content type on a page, I have to use view module. I don't like that.
Can I just specify the page and url when I create the content type?
But there is only one option, only front page. Can I change that? Hope you guys can tell me.
The views module is one of Drupal's killer features, you might want to reconsider writing it off completely.
Anyways, if you want to display all nodes of a type, you will have to code this yourself in a custom module. This means you will have to write some SQL, load the nodes and render them in a list. In this example you could use the EntityFieldQuery class to construct the query instead of writing the SQL yourself.
The end result will not be much different than what views will do for you, only difference is that you will need to create all of this with code in a module, instead of just setting this up in the Views UI.
I agree that views is a powerful tool that you shouldn't count out, but if you're set against using it (i refuse to use it on 2 of my sites purely due to overhead), You could use taxonomy instead. The taxonomy module already has a views style list for each term, I used this to achieve something similar to what you're looking for:
Set up a vocabulary whose terms match your node types and note the vid
add a hook_node_insert into your module file:
mymodulename_node_insert($node){
$terms=taxonomy_get_tree($my_vid); //where $my_vid == the vid of your vocabulary.
foreach($terms as $term){
if(strtolower($node->type)==strtolower($term->name)){
$items=array((array)$term);
$field['storage']['type']='field_sql_storage';
taxonomy_field_insert('node', $node, $field, null, null, $items);
}
}
}
After that you just need to add menu links pointing to your taxonomy page. Just as a note, I render all of my lists that I don't use views for with my own custom functions so I'm not sure what limitations this method might present.
I'm a drupal newbie...
I have different type of contents like News, Events, etc. and their content is different. News detail page has title-content text-date. but Events detail page has title-date-content text-location-speaker-etc. So I need different layout page for these different types. So, I enabled Drupal Themer to get a candidate name. for events page, it gave me page-node.tpl.php and it gives same for News page as well :( how can I separate these pages? I expected sth like page-event-node.tpl , but no... :/ Drupal Themer also give unique candidate name for event page like page-node-18.tpl.php but it doesnt mean anything since I can not create a general layout for all events by this node name. :(
Appreciate helps so much!! Thanks a lot!!!
While using different node.tpl.php files as suggested by monkeyninja (+1) would be the 'normal' way, you could add the functionality you want by adding page template suggestions based on node type yourself, in a preprocess_page function within a custom module/theme:
function yourModuleOrTheme_preprocess_page(&$variables) {
// If this is a node page, add a page template suggestion based on node type
if (isset($variables['node'])) {
// Build the suggestion name ('.tpl.php' suffix will be added by the theming system)
$suggestion = 'page-type-' . $variables['node']->type;
// Add to end of suggestion array, thus keeping the fallback to other suggestions,
// if this specific version is not implemented by the theme
$variables['template_files'][] = $suggestion;
}
}
With this in place, you should be able to add e.g. a 'page-type-event.tpl.php' file, which should be used for all event node pages.
(NOTE: You'll need to trigger a rebuild of the theme registry after adding that function to get it recognized by the system)
I'm not familiar with Drupal Themer, but a slightly different approach would be to work with the node templates to style the content and use something like the excellent Context module (and possibly Panels module) to change the layout of any additional information on the page (eg the blocks).
To theme the different content types using node templates, just create templates based on node.tpl.php in the form node-content_type.tpl.php. So you'd have a template for your events nodes called node-events.tpl.php.
You could then define a context using the Context module that reacted when a page of the events content type was displayed and select which regions/blocks you wanted displayed.