How to get node information from view in Drupal 8? - drupal

I created a custom block to display node author information - author name, published date, and category - in Drupal 8. I added the block to both node default and teaser displays. I am using following to get the node object:
$node = \Drupal::routeMatch()->getParameter('node');
It works fine when I'm on the default display (full node page) but it returns empty in teaser mode (views). I am thinking because the views do not have parameter information.
How can I get access to each node object from view display?
Thank you.
Seong Bae

Related

Drupal: display views page instead of default node/%

Case is that im using views for displaying each of content-types.
eg. q=?news/12 for news by nid
q=?product/13 for products by nid
Problem is when im creating (or editing) new content, it automatically displays (or refer) ?node/14 instead of ?product/14. Also menu link is created under node/14 link.
How could i set each view to referring content type?
Thanks in advance.
You can create a view (display mode : page) for each content type, which displays datas from your node (fields or whatever you need). To do so, add a contextual filter in your view, on content nid, give default value : content nid from url. Then give to this view a url like "/news/%". So "/news/12" hits this view and grabs data from news content type with id 12. Then you can manually modify menu link, or use pathauto to specify /news/[node:nid] as a pattern for all your news nodes (I never tried all this stuff together but it should work).
Other solution : https://www.drupal.org/project/contemplate (seems deprecated...)
Other solution : create a .tpl for your content type nodes : node--news.tpl.php, but no more views...
Other solution : create a view displaying one particular node data (like I explain first with a contextual filter), this view creates a block (display mode : block), you assign this block to a region (with block interface), and set this block to be displayed only for the news content type (last menu on the left on the block configuration page). Then with Display Suite you hide everything for this content type on the full content display. So on each node page which refers to a "news" node, you have nothing in your page but your block (and the node title, which you can remove with display suite extras I guess).
Good luck with it

Drupal View 's path. how to make drupal view's node link back to view's page rather than node type

I created a node type, say 'Project' with the path as '/pro/{node:title}', and then I created a 'Project view', in this view I show the project information as well as some other information by using the attachments and relationships. The path of view is /project/%, argument is project 's title
in order to avoid the conflict, so you see 'Project' node type is '/pro/{node:title}',
'Project' view type is /project/%
For this view, I created a page and a block, the problem is in the block, I want to make block's title link to the view 's page rather than node 's page. so that means, when in the view block, there is a link for the project, when I click this link, it will bring me to the 'project' 's view( project/% ) instead of 'project' node type(pro/{node:tile}).
I get my problem solved.
by working with view, I can use the option 'rewrite the output of this field' to add the link to the view instead of node type

How to assign view block to dynamic taxonomy term page for CCK entries

http://localhost/rkk/work-type/non-profit
I created a vocabulary and terms under for work-type. and I created some posts...
Then I created a View Block to list my entries, and assigned to a page... everything fine so far.
I display the work-type at entry details page. and I have a link "click to see other non-profit(assigned work type) works". When I click this link, it lists entries assigned to that particular work-type. thats fine, but it displays title/body/readmorelink :/ I need custom view for this page, like I did View Block for listing all posts...
Can I assign custom created View Block to this taxonomy entry list page? the url changes according to work-type, so how do I assign view to changing-url :/
Appreciate helps!! thanks a lot!
UPDATE FOR MORE INFO:
I have a vocabulary (work type), and terms under (corporate, non-profit, etc...)
I have a CCK for "work", and as a field I have select terms of work-type vocabulary.
I have a Custom Views Block to display works in a page (thumb, title, desc, link, etc.).
When I click a work, it goes to particular Work entry details page, I place a link in there "click to see other -term (corporate)- works", etc.
When I click this link, it displays the Work Entries under that particular term. it works fine. but it displays as default look (shown as below), but I need this page same as main works page list (thumb, title, desc, link, etc.) I already have a View for this, but how can assign this View to this dynamic url taxonomy page? I found the module http://drupal.org/project/tvi , but cant make it work.
It should be simpler to overwrite term pages with a view, there's already a pre-configured view for this : taxonomy_term (wich is disabled by default)

Drupal Views question

I want to display a different set of 2 or 3 images in a block on each page. So far, I have:
Added images to each page using CCK and filefield.
Removed the images from $content.
Created a view with node ID (Nid) as the argument, and added the field created with CCK, and added display type of Block.
When I try the Live Preview, it will display all the images for all pages in the block. When I enter the Node ID variable, it adds a where clause to the sql query and displays the correct images for that page.
How (where) do I set it up so that the view recognizes that it is on Node 3 and displays only the images for Node 3.
Thanks
John
"How (where) do I set it up so that the view recognizes that it is on Node 3 and displays only the images for Node 3." -- you can do this by clicking on the settings for the Nid argument in Views. Under Action to take if argument is not present: choose Provide Default Argument. In that, choose Node ID from URL
Position your block in the appropriate region (left sidebar, right sidebar etc). Make sure the visibility settings of the block are such that it only appears at paths that match node/*

drupal link to view dependent on argument

I have a node (node a) with which various other nodes (node b/c/d/e) references.
I can create a view with an argument as the node I'm viewing, (node a), and get a list of the nodes that are referencing that node.
Basically on node a viewing a list of node b-e.
I want to create a views page just for the node references of that node. But how can I pass on the argument to the new page?
Thanks.
Edit: I apologize for the confusing question.
Basically I have a movie node. I have many reviews referencing the movie node.
On the movie node I use argument: content: review_of to get a listing of reviews for that movie.
This is great, but because on the movie node there's only a few reviews, I need a page where I can see all the reviews per that movie. The question is passing on the argument for the movie from the view block (say, 3 reviews for the movie) to a view page (with all the reviews).
Ideally the url for the views page would be: /movies/movie-name/reviews
I have tried this with relationships as in Owen's answer, but was not able to make this work.
If I understand what you're saying, you're wanting to create a Views page that allows you to view nodes associated with a parent node (via a CCK Node Reference field)?
Once you setup your View, you can just pass the nid (or title, or whatever field you choose as your argument), to the URL.
So, assuming you have a Views page setup at: http://yourinstall.com/related-nodes
Just pass the argument in: http://yourinstall.com/related-nodes/5
The above (assuming your relationships / arguments are correct) will show all associated nodes with NID 5.
Here's an image of the views configuration I used.
You'll note the "path" setting on the views is "movies/%/reviews". I had two movies "Movie One" and "Movie Two", with a few reviews on each. You can then use http://yourinstall.com/movies/movie-one/reviews, etc... to see the titles of the reviews.
edit: Ok, so the clarification of the problem is, you have a Views block which displays a list of Reviews specific to a Movie. As well, you'd like the "more..." link to link to a Views page which display all the Reviews specific to that Movie.
This is doable via the Views admin, but does involve a bit of PHP code.
Your Views block can be setup similar to the above Views page.
Override your Views block Arguments (Review Of) Node: Title, and under "Action to take if argument is not present", select "Provide default argument" -> "PHP Code".
The code you use is:
if (arg(0) == 'node' && is_numeric(arg(1) ) ) {
$node = node_load(arg(1) );
return str_replace(' ', '-', strtolower($node->title) );
}
In essence, the Views block acts the same (expects a Movie node title), but will use the code above to try pull the title from the URL (note this works fine with path aliases). The Movie node title is placed into the more link as well (see the above image) in the format "movies/[movie-title]/reviews", which will link to the Views page as expected.
Am I understand this problem correctly?
The real problem is that the "more" link in the block doesn't link to the proper view?
In other words, If your view is "movie_reviews", and on the movie node page you have: movie_reviews->block added on the side, within this movie_reviews->block the "more" link directs visitors to movies/all/reviews ?
Have you considered templating these changes? (manipulating the link within the block to work with the node of the currently being viewed movie node.)
Blocks are very easy to theme.
I can give references on how to implement something like this if I this makes sense for your site.

Resources