How to add custom field to Basic page with particular node ID? - php

In Drupal, I would like to add a custom field to Basic page content type, but only to one Basic page with a particular node ID.
I know I can add custom fields to the whole content type but this is not what I want.
For example, is it possible to add a file upload field only to Basic page with node ID of 5?

Using Views you can create the list of nodes that you would like to display on the page. The new version of views makes it super easy to do that. From there you have a couple options, depending your level of skill with theming and how much control you need to have over where the list of nodes displays:
A Block - Create a block display for your view. Place the block in the same region as the main content block.
node--NODEID.tpl.php - You could add a template suggestion to your theme to override the specific node that you want to add the list to. To do that you would need to:
Create a node--NODEID.tpl.php file, replacing the NODEID with the nid of the node you want your list of nodes to show up on.
Embed your view in the node template using the following code:
nid); ?>
Where MY_VIEW is the machine name of the view you created and MY_DISPLAY is either "default" or the machine name of a specific display in your view.
Here is an example: http://api.drupal.org/api/views/views.module/function/views_embed_view/7#comment-32858

Related

What is the best way to create page in Drupal 8 so that I can then add these posts with images?

I have a Home page on the Drupal website (such as is created after installation), but I still need to create this page:
What is the best way to create a page so that I can then add these posts with images?
I am just starting to learn Drupal and have heard so far about such ways of creating pages:
1) in admin toolbar: Content / Add Content / Article
2) in admin toolbar: Content / Add Content / Basic page
3) in admin toolbar: Structure / Views / Add Views
Which one should I use? Or maybe there is some other option that I don’t know about?
P.S. At the moment I am more interested how to create empty page on which I can then add posts later, and adding posts it is another question.
Welcome to Drupal.
Drupal ships with the default theme which won't look nice but it does its job in the right way. Now if you want to create a better UI/UX obviously you should create a new theme. But before that make sure to read and understand the concepts behind Drupal. Drupal docs are your first friend.
Drupal Documentation
Drupal considers everything as nodes and that's how Drupal got its power. As you mentioned, Articles, Basic Page etc are called content types and they can be used to create a particular type of content.
Now for your purpose create a new content type and add the fields you need. From the image above I can say your content type needs Title, Image, Category and Date. After creating content type you can create as many contents as you want under the content type you just created. Consider each card in your image as content.
Now you can use a Drupal Core Module Views, to perform DataBase Operations without writing single code. Yes, you can select fields, sort, order etc with Views UI and display it in a page or a part of a page (Block).
I would say just try this out in the default Drupal theme and when you understand how this works, you can start creating your own theme for your project.
Theming Drupal
There is a lot of resources available. But you have to make sure what you are asking is whether you actually need. It will take some time, but it worth.
To build layouts for homepages on Drupal 8 you best friend is https://www.drupal.org/docs/8/core/modules/layout-builder
To build the content blocks inside your home page, you should start creating nodes on a node content type to hold your information. For instance: news content type, with a title, a body, a date, and an image.
For every node type, ex. news, work on the preview display, full display, and any other display which makes sense. These displays can be used later in the Layout Builder directly or in Views, referenced below.
If you wish your list to be dynamic, such as the last 10 entries are shown first, then use a view to hold the content sorted and filtered as you need.
In a nutshell.
Create a content type for your article/news.
Modify the displays of the content types to have at least a summary and full view.
Create the content itself to have something to see.
Create the view (block) to filter and sort your content.
Create a page layout (this makes sense for landing pages) which places your new view and any other content you need in any disposition.
This is roughly what I would do. The steps described above contain many intermediate steps. If in doubt, check the docs.
I hope that helps!
First, decide what will you display on that page. Is it content in some existing content type (article maybe) or you want to crate new one for this purpose.
If you need new one then create it (Structure -> Content types -> Add content type).
Then check what fields will you need. I.e. image, some description text. Add missing ones.
Create few nodes (pages) in that type so you could work with them.
Then for displaying you should crate a view (Structure -> Views -> Add view). It can be a page view (you are displaying only that content on page) or block view (this is just a block among some others). If you create a page you could visit it and if you create a block you have to add it to some region to appear on page (Structure -> Block Layout).
Inside your theme you should create templates for this page/block. Turn on twig debug mode so it will show you hints - what templates are used and how can you name yours to override default ones.
Adjust CSS to make it look like you want it to look.
Find some tutorial(s) for the details
Previous answers have given the flow of the work you should go through, I would like to add some resource that might help you achieve this.
Creating content type and fields: https://www.drupal.org/docs/administering-a-drupal-site/managing-content-0/working-with-content-types-and-fields
https://www.drupal.org/docs/user_guide/en/structure-content-type.html
View and View modes: https://www.drupal.org/docs/user_guide/en/views-concept.html
https://www.drupal.org/docs/8/api/entity-api/display-modes-view-modes-and-form-modes
Handling block of the view: https://www.drupal.org/docs/8/core/modules/block/overview
Feel free to ask if any further explanation is needed.
Thank you

Drupal 7 show current displayed node field in block

Can someone pls guide me with this?
I have five images associated with a node which I have kept hidden. I want to show them in a block.
How do I show the images in the block belonging to the CURRENTLY DISPLAYED NODE?
So that as the node changes, it will automatically load the images of that node on the block?
I figured this has got to do with adding filters.
But I cant seem to get any
'current node' filter as such when I was seeing those lists.
I also read that I can pass an argument of the current node to the block. But In drupal 7, there is no such field called 'arguments' while editing the block....
Thanks
You can do that using views.
1- Create a new views block.
2- Under Fields section, choose your images field.
3- Under Contextual filters click Add then choose Content: Nid
4- Under When the filter value is NOT available choose Provide default value then Content ID from URL, then click Apply.
5- Save your view and go to the blocks manager page admin/structure/block and click configure beside your new views block.
6- Navigate to the bottom of the page to Content types, then check the content types you wanna have this block to be shown.
Hope this helps... Muhammad.

Do Views Attachments require changing the Drupal templates

I've created a drupal views Attachment display that lists a bunch of nodes. How would I add this to a content type? I've seen examples that require changing the template files, but can views Attachments be used without making changes to the theming layer?
Views attachment displays attach the display to another display within the same view: look at the Attachment settings block on the attachment display. The use case for this is if you had, say, a summary view and a detail view that had to appear together: you'd attach the detail view display to the summary view display.
If you want a view to show up on pages other than the view itself, you need to create a block display. Then, you have two choices:
If you want it to show up in a region defined by your theme (i.e. as a regular block), just go to Site Building -> Blocks and move the newly created block to whatever region you want.
If you want it to show up as part of a node, you need View Reference, which creates a CCK field that lets you select a view. That field can be positioned anywhere in your node template like any other CCK field.

how can I output the link or path with drupal views and customfield?

I have a view which lists some nodes and I want a custom output for the title of the node. Im using customfield which allows me to write PHP code but I cant find how to output the link to the node? It doesnt appear in the $data object. I can add the field, but still cant use it in the customfield code
Rather than using Views Custom Field to load PHP into the database, consider modifying the views template for the title field. If you go to Theme Information, it'll give you the file hints for the field. Create the file in your theme, and modify the title that way.
This has the benefit of separating code from content, and if you do it this way, you can add the Note: Link field, exclude it from the display, and use it in the Title template you created.
Check out Group 42's Guide to Theming for more information.

How to place multiple content types on a Drupal page?

I've created a new content type called Homepage, which has a number of fields. The fields are node references to the Single Image Promo content type. Single Promo Box has fields for title, text and image. The idea is that I can create multiple copies of a Single Promo Box, each with different field values. I then want to place a few of them on the homepage.
So I can place a specific Single Image Promo on the homepage fine now. But what template file do I need to create to style the way Single Image Promo is shown when it's a node reference on the homepage?
Thanks,
Maria
It sounds like you are over complicating things.
If you want to create some different boxes on your homepage, using node reference is not the right way to go about this. There are some few different ways to do what you want, the easiest i probably to create a block view of the node you want to display. When you turn a node into a block, you get the ability to place it in any of your theme's defined regions.
You could also just create a view and from there get the different Single Image Promo you want without using node reference at all. Views has a lot of different filters, which will enable to you create a view that always displays the right nodes. This solution wont work well, if you want to place the nodes in different regions though.
To answer your question, when nodes are being displayed, they will by default be rendered with the node.tpl.php template file. To make specific templates for each of your node types, you can create a new template named node-nodetype.tpl.php, where nodetype is what you called the node type when you created it.
node-nodetype.tpl.php can work to a certain degree, but in that file you are styling the entire page, with the content type's fields delivered wholesale to the tpl via a single $content variable. Maybe it is enough for you, but if you need finer grained control:
You can edit and style the individual content type's fields with the Content Template (contemplate) module. It provides access to all of a content type's fields within the $content variable (which is delivered wholesale to the node-nodetype.tpl.php. The contemplate is php, so you can add logic and conditional formatting for the node-reference fields (usually clearly labeled in the contemplate).
And don't overlook the simple solution of the content type's 'display fields' tab (next to manage fields tab), where you can control some field outputs right out of the gate.

Categories