Show content of referenced field in a block - drupal

I have 2 content types in Drupal: Articles and Newspapers. The Article type has a a reference field which points to the Newspapers. Each Article can only reference one Newspaper.
So an article called Sunday reports belongs ta a newspaper The Times, article Monday reports belongs to The New Yorker. I created a view and inside that view a block. So when a user visit that article page it should display in a block to which newspaper it belongs.

In Views...
Add a "contextual filter" of "content:nid".
on this filter, set "WHEN THE FILTER VALUE IS NOT AVAILABLE" to "Provide default value",
Set "Type" to "Content ID from URL",
Then add a "Relationship" to your entity reference field
Then in the output fields, add NID and set it to use the relationship.
I am not sure what version of drupal you are using, but it should be basically the same for D7 - D9

Related

Relating different content types in Drupal 7

I've just started learning Drupal 7 and I'm stuck on a problem.
I want to relate different content types using taxonomy. For example, how can I display blogs related to an article?
I know how to relate articles and basic pages but I don't know how can I relate blogs with articles?
Please help me.
Use reference field in blogs content type
like
field name is: related with
field type is reference
set reference type node
set node type is article
optional multivalued true
Now you have a relationship field in blogs
Now you can do every thing with this relationship
Hope this make sense
Thanks
You would create your taxonomy vocabulary and supply it with terms. On its own, this doesn't do anything, you need to add a field to each of your content types that are using this taxonomy.
If you go back to your content types, structure > content type > article. Here you would create a new field of type term reference. Inside the settings of the field you'd select your vocabulary and then also the widget eg. select list. You can re-use this field on the other content types where you want to share this vocabulary.
If you go back to your content for the types you added this field for and populate them with selections, when you view that page by default the term will appear as a link. If you click this link it will take you to a page view that will list all the other nodes that have that term form that vocabulary.
More information on taxonomies: https://www.drupal.org/docs/7/organizing-content-with-taxonomies/about-taxonomies
If you are creating a view and want to know how to pull in different tagged content there, I'd recommend reading this post which outlines the steps to do so https://drupal.stackexchange.com/questions/205921/how-do-i-show-related-content
The process:
Basic stages:
Create a “Content” view-block.
Add a contextual filter: “Has taxonomy term ID”.
Choose “provide a fixed value”.
(From type): “Taxonomy term ID from URL“.
Checking:
• Uncheck “Load default filter from the term page”. • Check “Load
default filter from node page, that’s good for related taxonomy
blocks”.
• Check “Limit terms by a vocabulary”. • Check your desired
vocabulary.
Select “Filter to items that share any term”.
Go down and check “Reduce duplicates”: This will several terms that relate to the same page – To appear. Only one will of them will.

fields of content type show as sidebar menu in drupal

I am newbie in drupal I learned how to create content types, but I need do it:
I have the content type project with fields location, arquitect, images, contact, saler, etc each field is text with google maps reference, the profile of arquitect responsable, imageg of project, info of contact, saler profile respectively I need to show this info in this way. How Can I do this? I am usin Drupal 8
You can achieve this by installing and setting 'Views' module:
https://www.drupal.org/project/views, then to create new view with block display that will show fields only for current node ID of your specific node type by using contextual filter in advanced section when editing views:
You can setup your view block like this:
Add in contextual filters node nid:
For this contextual filter, select provide default value of type PHP code and set to retrieve it through arg(1):
When you finish setup your view, your block will appear under blocks, so you can assign it to region you want to, i.e. sidebar.
If you want to, you can restrict visibility for specific content type.
For drupal 8 it is content -> ID, then select "Provide Default Value" when the filter value is not available, and under type select "Content ID from URL"
Under section advanced, add new contextual filter
Choose Content: ID
Choose provide default value and Content ID from url

In Drupal 7, can you create a field in a content type which lets you add content of another content type and keep the reference?

Imagine the following content types:
Content Type A
- Title
- Field with a link "Add content of type B"
Content Type B
- Title
- Node Reference
If one adds content of the content type A and views this node, there shall be a Link/what ever which says "Add content of type B". When clicking this link (/node/add/b), there shall be a direct reference to the previous node of type A. For instance:
User adds new content (type A) titled "Awesome content" with url node/10. Some other user views this node, sees the link "add content of type b" and opens node/add/b. In this node form, there is the field "Node reference" and shall automatically preselected node/20 because this is the node the last user came from. Is this possible?
I haven't seen a module that does this.
But you could do it by having a link on node A, but instead of it being 'node/add/b', you could make the link 'node/add/b/[node A id]'.
Then on node b have a node reference field and set the default of this field with a form_alter and get node A's id with arg(3) in the form_alter.
I think this new module might help you,
Inline Entity Form
this gives you the ability to edit ContentTypeA node which holds an inline field that is reprsenting ContentTypeB node.
all this from the same page.
add to this, from that field you can add multiple fields and nodes I think.
there is youtube video that explain it
Nodereference URL widget does this magically awesome.

Change block content based on changes in another block

I am experienced Java/C++ programmer, but totally new to Drupal/PHP.
Short question:
How do I refresh all the blocks in my page, based on the input to a particular block?
Exact Scenario:
I am looking to create a website with display and behaviour similar to http://www.google.com/finance. I have started creating a custom module for this in Drupal 7, So we want to have:
a top input block where users can enter a particular company's name.
a main block which starts with general content (e.g. tables plus latest news about the economy etc). As soon as a company is selected in block 1, this changes to news and tables about the chosen company.
a side graph block displaying some relevant graphs etc. Again, when no company is chosen, this could display the general graphs (e.g. S&P, Dow Jones, NASDAQ), and when a company is chosen, this displays the details for the chosen company.
The way I see it, the website works in a "current context" for the user. So, perhaps I can set a session level variable in Drupal, and refresh all blocks based on the current value of this variable?
However, I am not sure how to achieve this, and what is the best way to do this? (AJAX? Taxonomy?)
Any pointer, hints, suggestions, examples, sample code are most welcome.
This is how I would approach this problem based on how you have described it. The majority of the functionality being handled by the Views module.
I'm assuming you have the following setup:
A Taxonomy vocabulary called "companies" which has company names as
terms.
A Content Type called "News", which has news information about
companies. Most importantly it will need a taxonomy field where you
can select which companies it is related to (lets call this field "company_reference")
A Content Type called "Tables"(?). I'm not sure what information you
want in your "tables", but again it's most important that is has a
taxonomy field to reference companies. (can be the same field "company_reference")
The Majority of the functionality you are looking for can be built using the Views module.
I would create a View (let's call it "Company Data"). the view is going to have three different displays, each of type "block".
Display 1: Input Block
Set the display name to something meaningful, say "user_input_block"
For this block, leave FORMAT settings as they are.
For the FIELDS settings, just have "Content: Title" (does not
really matter for this block).
For the FILTER CRITERIA settings, add a filter and select your
"company_reference" field, set it to auto-complete, expose the filter
to visitors and (under the "more" section) change its Filter Identity
to "company".
For the PAGER settings set it to display a specific number of items and set it to '1'. (this will limit the data this block retrieves)
Under the Advanced section change the "Machine Name" so comething meaningful, say "user_input_block"
Still in the Advanced section click on "Theme: Information". this will display a list of the different custom template files you can have for this view. For "Display Output" write down the last template suggestion in the list, it will be something like "views-view--company-data--user-input-block.tpl.php" ("views-view--{your view name}--{your display name}.tpl.php"). Click on "Display Output" and copy the PHP it lists. (this is the views default PHP for the view).
In a text editor/IDE (whatever you use) paste the copied PHP code and save it in your custom theme with the template name you wrote down. Edit the PHP and either comment out or remove the section that says "<?php if ($rows): ?>...<?php endif; ?>" (this will remove the returned content from the display)
So to review the view display that was just created will (using the custom template) display a block with just a field that as the user is entering a company name it will autocomplete. It will then submit the form and pass it as a GET variable to the current url (www.yoursite.com/yourpage?company=users company").
Display 2: ** Main Block**
Set the display name to something meaningful , say "company_news".
Set the FORMAT settings to which ever you like (or leave as is)
For the FILTER CRITERIA add a "Content:type" filter and select your "News" and "Tables" content types.
In the Advanced section click "add" next to "Contextual Filters". )A contextual filter is passed in the URL, we are going to be grabbing the value that was passed from the Input block.) Select "Content: field_company_reference" as the field and click "Apply"
for the "when the Filter value is Not available" section select "provide default value"
for the "Type" select "PHP code" and the PHP code will be something like this "return isset($_GET['company']) ? $_GET['company']:false;"
for the "When the filter is available of a default is provided", check "Specify validation criteria", set "Validator" to "Taxonomy term", check the "Companies" vocabulary and for "Filter value type select "Term name converted to Term ID"
for "action to take if filter value does not validate" select "display all results"
To review, this view display will display a block that lists "news" and "tables" content. If the GET variable "company" is passed (from the input block) then the content of this block will be filtered to display only content that is associated to that block.
Display 3: ** Graph Block**
I'm not exactly sure what data you want to display in this block (or rather where it would be coming from) but if you set it up like how I described setting up the Main Block it will filter by company (use the same contextual filter and content that has the company taxonomy reference field).
The one difference with this approach is that it requires the page to be reloaded when a user enters a company into the input block. If you want to avoid this then you could make the following changes;
for the main block and graph blocks instead of using a contextual filter use a regular filter for the Company reference field (same settings as were setup in the input block). For both these blocks under the Advanced section, set "Use AJAX" to "Yes", and use custom CSS to hide the exposed filters from view.
with a custom module (or with your custom theme) that adds additional JS to the page. Have the JS triggered by the field in the input block, when a user enters a value into it, the JS copies that value into the exposed (but hidden) field for the other blocks, triggering Drupal to reload them via AJAX with the passed filter.
I haven't tested any of this and it is just theoretical based on my knowledge of Drupal, but it should start you out on the right path.

Drupal 7: Create glossary with views, but sort it by a field other than the title?

I am trying to create a glossary view but the one that Views provides uses the first letter of the node's title. I need it to use the first letter of a field named Artist in my content type. I've tried making a new Contextual Filter and match it up with the one provided by the glossary view but the Glossary mode is not even an option, which I find to be very odd. Anyone have any ideas? :-/
You're doing the correct thing by copying the existing Contextual Filter, for both Page and Attachment displays. Your main problem is figuring out why the Glossary mode option doesn't appear for your field. With Views 7.x-3.3, it should do if your Artist field is a Text type but I suspect yours is actually a List or Term reference.
If it's a List (text) field: upgrade to Views 7.x-3.x-dev and the glossary mode option will be available.
If it's a Term reference field: add a Relationship for the Term or "Taxonomy terms on node" and then you will be able to use the Term Name as a glossary argument.
If it's a Node reference field: add a Relationship for your node reference field and check "Require this relationship". Then update the Content: Title contextual filter in both the Page and Attachment displays to use this relationship. This tells the view that the filter should be based on the title of the related node instead of the parent node.

Resources