Questions tagged [nodes]

The most basic form of content on a Drupal site is a node.

Filter by
Sorted by
Tagged with
51
votes
4answers
74k views

How do I get the current node ID?

In Drupal 7, if I wanted to get to get the node id of the currently displayed node (e.g. node/145) I could get it with the arg() function. In this case, arg(1) would return 145. How can I achieve the ...
51
votes
13answers
20k views

How do I hide nodes that shouldn't be directly accessed from users and search engines?

I've found that there are times I have a node that simply contains content that will be displayed somewhere else, but shouldn't be viewed directly. That is, no one should ever go to node/1234, but the ...
48
votes
9answers
58k views

Where does Drupal store the content of a node's body?

In the node table, you'll find everything except the actual body. Where can I find this body text? I'm assuming it's being stored as a blob somewhere.
41
votes
11answers
28k views

How to display a field before node title?

I'm looking to render a certain field before the node's title when displaying a teaser in D7. Effectively I want to go from this to this. I have looked into updating the node.tpl.php but I notice ...
39
votes
9answers
29k views

Count of nodes by type [closed]

I am looking for a snippet that lets me display the total count for a specific node type, e.g. "Pages = 167" or "Products = 10630." What code should I use to achieve this?
39
votes
3answers
52k views

How do I customize the search result page?

I have customized the teaser view of my content types and the page view. Now I need to use the teaser view for the search results page. By default, Drupal displays a full text jumbled mess for each ...
37
votes
2answers
29k views

How to get a node by its url alias?

I know I can load a node by its title with the node_load() function. Is it possible to load a node by its URL alias?
35
votes
5answers
28k views

Is there a Drupal API function to get the list of nodes by type?

Is there a Drupal API function like node_load() that will return a list of nodes by a given type? I've tried $nodes = node_load(array("type" => 'student_vote')), but it only returns one node. I ...
34
votes
7answers
44k views

Programmatically create nodes

How can I programmatically create nodes with date and image fields? I know I can do it in Drupal 7 with the following code. global $user; $node = new stdClass(); $node->title = "YOUR TITLE"; ...
32
votes
6answers
30k views

How to load a node based on its title?

I only know the title of a node, and I want to load the node using node_load(). How do I do that?
31
votes
17answers
32k views

How to delete all nodes of a given content type?

I have several thousand nodes of a particular content type. Using the web interface (example.com/admin/content), I can only delete about 50 at a time. How can I quickly delete them?
30
votes
7answers
19k views

How do I let users unpublish and publish their own content?

I want to let users unpublish and publish their own content. By default, you have to give "administer nodes" permission for users to be able to unpublish or publish content. That is way to broad ...
26
votes
8answers
51k views

How to programmatically get the NID of the current node

I've pored over this old thread on drupal.org and it just kind of makes my head spin around. Pulling down the path and trying to parse out the NID from within it? There has to be a better way. And ...
25
votes
6answers
25k views

Programmatically attaching files

I have created the "Gallery" content type, and added two fields: "photo," and "document." I then used the following code for uploading a file in the "document" field: $file = file_save_upload('...
24
votes
5answers
8k views

Unable to delete feature content type after uninstalling feature

Issue Uninstalling a feature which creates a content type hides the "Delete" link on the admin/structure/types. Issue on Drupal.org
22
votes
7answers
35k views

Get path alias from NID (or node object)

I have a node object, and I am trying to use it to get a given path in Drupal 8. There was drupal_lookup_path() in Drupal 7 but I'm not seeing any equivalent or documentation for Drupal 8. What's ...
22
votes
7answers
16k views

How to programmatically set the front page?

I want to set the Drupal front page programmatically at run time. How can I do this? Is this possible?
22
votes
3answers
29k views

How do I get the node URL in a Twig template?

I am trying to get the node URL in a Twig template. {{ content.node_link }} returns the full link, but I only need the URL. How do I retrieve the node URL?
22
votes
14answers
12k views

How do I hide the default page /node?

Drupal out of the box comes with a page (which is the default homepage) that is assigned to the node path. How do I avoid users reach it, and redirect them to the actual homepage of the site?
21
votes
5answers
27k views

Get all nodes of given type

I would like to get all nodes that are of my_custom_type type in Drupal 8. I know I can get all the nodes (of any type) with \Drupal\node\Entity\Node::loadMultiple() and the list of all types by \...
21
votes
2answers
20k views

How to iterate over all the nodes from a content type

I want to recover some info from a field of all the nodes of a specific content type. I know how to do it using a db_query and getting all the node ID then iterate over them. What I would like to know ...
21
votes
4answers
30k views

How to import / export content type?

In Drupal 6 we had content type export import, I cannot find this in Drupal 7. Any tips please?
21
votes
4answers
11k views

Relation vs. References vs. Entity Reference for managing node-reference-type relationships

References Entity Reference Relation It seems like there are several active modules that all have similar functionality. Can anyone offer any information as to which branch appears to be the most ...
20
votes
9answers
13k views

How to implement two way node references?

The node-reference module (a component of CCK) provides a great way to create a reference one node from another. What is the best way to expose a two-way relationship between the two? Example below: ...
20
votes
1answer
15k views

How do I get the name of the content type?

in Drupal 7, which function do I need to call to get the name of the content type? I have the $node object and I also have the id of the content type if I use $node->type. But I need to get the ...
19
votes
7answers
21k views

How to copy content type?

How to copy content type in Drupal 7 ? I can't find a button to duplicate content type.
19
votes
4answers
13k views

Why can't I delete certain content types?

I have a Drupal 7 site, and under the Content Types menu, only certain types have a 'delete' link under the 'Operations' column. All of the content types have the links for 'edit', 'manage fields', ...
19
votes
2answers
21k views

How to load a node by a field value?

I want to load a certain node. I don't know the id of the node but I do know that it has a certain hash field that is unique and I know that hash. I want to load the node via the hash field. How can ...
19
votes
3answers
15k views

Embed a “node add” form in a page

I'm looking for a way to embed a "node add" form on a landing page. I was using Form Block, but it appears to bring many error because I was using a development snapshot version.
19
votes
7answers
12k views

fast saving single field value

I've got around 70k nodes of specified type on my site. I need to run an update on them. Some operations and setting one field to desired value. node_save is really slow and it causes crashes (too ...
19
votes
4answers
30k views

How do I add additional view modes for a node?

I am creating a custom content type. By default, nodes support only two view modes (full and teaser): function mymodule_view($node, $view_mode) { if ($view_mode == 'full') { $node->...
19
votes
5answers
21k views

Programmatically updating a node

I can create a node using the following code: $node = \Drupal::entityTypeManager()->getStorage('node')->create($array); But if I have the node ID, how can I edit a node?
18
votes
10answers
26k views

How do I change the machine name of a field once it's been created

I spaced out while typing the field name an now the machine name is not what I wanted it to be. I know it's just an annoyance. but I'm wondering if there is some way to alter the machine name of a ...
18
votes
4answers
17k views

How do I remove the “submitted by” text on node display?

In Drupal 6 the option to remove that text was located in the theme settings; where is it in Drupal 7?
18
votes
5answers
20k views

Is it possible to restore a deleted node?

While revisions are kept and it is easy to get back to a previous revision of a node, I was wondering if it is possible to restore a node that has really been deleted by a user?
18
votes
4answers
20k views

How to set a taxonomy term programmatically for a node?

I created taxonomy terms and assigned it to a content type 'Article' using term reference field. I created nodes by programmatically but how to set taxonomy terms for the nodes? The taxonomy terms ...
18
votes
8answers
9k views

“View unpublished content” permission for non-author

I googled a bit to see if there's a permission-level way to allow a logged-in (but non-admin) user see unpublished content (marked with the CSS node-unpublished class). This appears to be scheduled ...
18
votes
8answers
17k views

Limit which roles can view a node basing on its content type

I'm using Drupal 7 and I created a new content type called "eBook," and I created a new role called "MonthlySubscriber." My intention is to allow only users with the "MonthlySubscriber" to view the "...
18
votes
6answers
7k views

View of users flagged by a user, users flagging a user, and nodes from users flagged by a user

I have a user flag that is pretty much like a "follow" or "subscribe" function. Basically I want to create 3 views for my website's user profile pages: A list of users flagged by a user (e.g. a ...
17
votes
2answers
11k views

How can I detect when a node is changed from 'Published' to 'Unpublished'?

In hook_node_update(), I can easily tell the 'status' of a node (whether it's published or not) after it's been updated. However, is there an easy way to check to see if the updated node was ...
17
votes
5answers
15k views

How I can delete all node/field revisions?

I have a drupal 7 sites, where it keeps node and field revisions. I keep revisions for the case where by mistake a new faulty version is saved and I want to revert back. But, now I want to clean and ...
17
votes
9answers
24k views

How can I create a view of child nodes based on the menu relationship?

Say you have the following pages in a menu tree: Projects Project One Project Two Project Three I'm looking for a way to create a view that will be embedded in the Project (parent) page that will ...
17
votes
1answer
1k views

How to correctly remove a module in a staged environment?

Some modules have deïnstallation routines. Which typically remove databasetables for that module, variables from the variable table and locales introduced by that module. These routines live in the ....
17
votes
1answer
731 views

Sharing content with domain access?

I setup a site with domain access. It has 2 subsites. I want to publish content from the main site to the subsites. This is already working good with send to affiliates. But I want some local admins ...
16
votes
3answers
11k views

How can I get the node ID from a path alias?

I want to be able to get the nid but the only information I have is http://example.com/this-is-the-alias. Is there a way to get either the internal path of /node/* for that alias if I am not on the ...
15
votes
9answers
14k views

How to disable 'promote to front page' and 'sticky' options?

Two possible methods that I have explored: Use hook_form_alter to unset both options on node edit forms. But on the main admin content page, the options still appear under 'Update options' dropdown. ...
15
votes
13answers
14k views

How to remove the “No front page content has been created yet.” message from the front page?

I only want blocks on the homepage. Because I don't have any nodes, and I want to keep the homepage as /node I am getting a "No front page content has been created yet." message on my page. How do I ...
15
votes
7answers
303 views

Is it possible to make a second “edit account” page that only allows users to edit some profile fields?

So here is my situation: users on my site have to fill out a rather large number of fields upon registration. However, after they register, only four (out of about twenty) of these fields need to be ...
15
votes
4answers
3k views

Delete a node after X weeks it was created

How can I delete a node after, for example, 2 weeks it has been created?
15
votes
3answers
21k views

What hook can I use for post-save actions?

I've found several posts with this same question, but the answers always diverge to some other topic or they don't work for my needs. After a node is saved, I want my custom module to insert a row ...