Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [entities]

Nodes, comments, taxonomy terms, and users, using the terminology used since Drupal 7, are generally called "entities."

83
votes
7answers
15k views

When is it appropriate to create an Entity versus just adding a new content type?

What is the benefit to creating new entity types over just building a new content type? It seems a bit overkill to do all of the custom coding that is required to create a new entity when you have ...
71
votes
4answers
67k views

How do I load a field collection from a node?

I have a node that has a few field collections on it. When I look at node_load() output, I see: [field_rich_text_group] => Array ( [und] => Array ( ...
57
votes
9answers
110k views

How do I access a field value for an entity (e.g. node) object?

node_load((int) $nid) loads a fully-populated node entity. $node->title->value gets the title, but how do I access field values in Drupal 8?
55
votes
8answers
47k views

How to programmatically create Fields?

How can I approach implementing the following in Drupal 7? What I need to do is create a module which defines a new fieldable entity, called 'Company'. I have a list of, let's say, 20 fields that ...
51
votes
3answers
10k views

What's best practice when working with the language[und]?

I'm starting to use this form to access data in my module. (See comment #1.) $node->field_test[$node->language][0]['value'] I thought that looked like a pretty good solution, but further down ...
46
votes
7answers
38k views

How to change the length of field settings?

I have set once to a website a lenght limit to a field. And now the client wants to put more characters to that field. I cannot change the maximum size from Drupal because I get the following error ...
46
votes
3answers
28k views

How to provide template for particular view mode?

I want to theme the search result view mode with a template. I had it in my head that naming a template file node--article--search-result.tpl.php would do the trick, but I'm obviously wrong about ...
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 ...
40
votes
12answers
25k views

How do I remove a configuration object from the active configuration?

I'm writing a custom module for Drupal 8. It defines a field storage configuration object, in a file called modules/mymod/config/install/field.storage.bloom_stem.bs_twitter_ac.yml. The problem is ...
38
votes
7answers
9k views

Is there a good way to inspect objects whose type is derived from the EntityDrupalWrapper class?

I'm sure this question applies to other complex object types, but EntityDrupalWrapper is the one I'm currently working with. This is the type of object returned by entity_metadata_wrapper() (and ...
35
votes
5answers
16k views

Bulk field update?

I added a new (text) field to a Drupal 7 content type that already has many nodes. How can I fill the field with a default value for all these nodes?
34
votes
11answers
33k views

Get all users with specific roles using EntityFieldQuery

I thought this was an easy task, yet there does not seem to be a Drupal-method for this. I came as far as knowing I have to use EntityFieldQuery for this - because the API said conditions for ...
34
votes
9answers
5k views

What's a good balance between reusing fields versus creating new ones in the context of fields scalability?

I've read the following phrase on a website: Instead of adding new fields to a content type, adding existing fields is a better option to reduce the system’s complexity and to improve scalability. ...
31
votes
4answers
37k views

How to get instance of referenced entity?

I have a node object with image field named field_image. When I do $node ->get('field_image') ->first() ->getValue() I get array with some properties instead of entity object. The ...
31
votes
3answers
11k views

Differences between value and safe_value

What are the differences between ['value'] and ['safe_value']?
30
votes
5answers
27k views

Best practice for language key for “und” in hook_form_alter

I'm modifying in a form in my custom module. Code looks like: function my_module_form_alter(&$form, &$form_state, $form_id) { if ($form_id == "someID") { $form['...
26
votes
6answers
14k views

Debug EntityFieldQuery?

I have a module which is misbehaving. An EFQ is returing unexpected results, but I can't see why just by looking at the code. Is there a dpq() equivalent for EFQs? Other ways of debugging them?
26
votes
1answer
18k views

How to create a view type of Entity reference display?

I have an entity which contains a field collection of entity references and can edit the entity references with the Entity selection mode is Simple (with optional filter by bundle), but only Entity ...
25
votes
1answer
18k views

How to check if field exists in an entity?

In entity node, I want to check if 'abc' field exists or not. If exists, I will get its value by $value->get('abc'). But the content type where this field doesn't exists, gives error: The website ...
25
votes
5answers
19k views

Using OR with EntityFieldQuery

I have never had the need to do this before today, but it doesn't seem like you can make OR queries with EntityFieldQuery, since db_or is used for select queries. An example would get to all entities ...
25
votes
4answers
854 views

With current D8 status, what's the decision tree for creating a new content entity type versus creating a Content Type for the “Node” content entity?

We have seen four years and Drupal 8's first release since the accepted answer was written for the question "When is it appropriate to create an Entity versus just adding a new content type?" And, ...
24
votes
7answers
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
3answers
13k views

Which should I use and when: node_load() or entity_load()?

Title says it all. I understand that entity_load provides a performant and efficient means to load multiple nodes whilst reducing database queries, so I presume in that instance I should be using ...
24
votes
5answers
17k views

How to set value of file field with entity_metadata_wrapper

How do you set the value of a file field when using the entity_metadata_wrapper? I've tried doing this in code: $file = new stdClass(); $file->uid = 1; $file->uri = $file_path; $file->...
23
votes
1answer
8k views

What is the purpose of the entity_metadata_wrapper() function, and why should I use it?

I seem to be developing add-ons to a lot of modules that use Entity API at the moment, and the entity_metadata_wrapper() function keeps on popping up. The docs page says this about it: Returns a ...
22
votes
5answers
35k views

Hide a field label

Is there a way to hide the field label when rendering a custom field in Drupal 7? In my node template I have: <?php print render($content['field_image']); ?> Which gives me: <div class="...
22
votes
8answers
18k views

Checking for existence for a field on an an entity_metadata_wrapper

I am iterating through a field collection, and wrapping the field collection items with an entity_metadata_wrapper. I would like to check for the existence of field before calling its value method (...
22
votes
2answers
22k views

Programmatically update an entity reference field

I know I can update (or set a value for) an entity reference field in Drupal 7 with code similar to the following. $node['field_code_used_by'][LANGUAGE_NONE][0]['target_id'] = $id; I want to ...
22
votes
2answers
4k views

Are there any benefits/caveats with using Entity Reference over Term Reference?

Pretty simple question really, but is there any benefit to using a core term reference field over an Entity Reference field? Obviously tagging is not really an option with Entity Reference, but I've ...
21
votes
5answers
35k views

How to update a field value for a node?

I want to change the value of a field for a node.I used the following code: $node = node_load($nid); // $nid is the function's parameter $node->field['field_myfield'][$node->language][0]['value'] = 1;...
21
votes
5answers
5k views

Is displaying a working Field Widget Form on its own possible?

I'm interested in embedding a Field Widget Form out side the context of the whole node edit form. I have displayed full forms in the past using drupal_get_form but that doesn't seem to apply for ...
21
votes
2answers
10k views

Best way to drop a field from a content type

What is the best way to remove a Drupal 7 field from a content type defined via the GUI? I want to use hook_update_N so the changes can be propagated to multiple servers upon running update.php. ...
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
5answers
34k views

How to add extra fields to user profile?

I'd like to add additional fields to the user profile. I'd like user to be able to edit almost all the information I store in the database about him. I have some ideas: maybe it's possible to do ...
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
3answers
44k views

How do I get the raw field value in a twig template?

myfield is a plain text field. {{ content.field_myfield }} renders the field with HTML markup and the field label. How do I render the raw value of the field? {{ content.field_myfield.value }} doesn'...
20
votes
4answers
10k views

How do you clear a field value with entity_metadata_wrapper()?

Suppose I have an entity with a field field_foo on it and I want to clear that field's value. What should I pass to $wrapper->set()? I have tried NULL and array() and both produce an error ...
20
votes
3answers
14k views

How to hide a custom user field in user profile?

In Drupal 7, what is the best way to prevent any non-administrative user from changing a custom user profile field? Use case: I've created a user field called "Customer ID" that I do not want the ...
20
votes
3answers
14k views

How to change a field value with a Rules Action?

I'm trying to set up a Rules Action that can change the value of a field. I created an Action Set with a data type of Node. When I go to create a Rules Action, I have access to every default Drupal ...
19
votes
5answers
30k views

How to add additional display(view mode) for 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
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
19k views

Get only some fields with EntityFieldQuery?

Drupal 7 has very promising ORM-like system of queries with EntityFieldQuery. Currently, I understand how to make selection of nodes, but the result doesn't contain any specific information, like ...
19
votes
4answers
8k views

EntityFieldQuery vs Db_select()

Why should I use EntityFieldQuery when I can do the same work with Db_select() to fetch the value. It would be better if someone can provide an example, not only a link.
18
votes
4answers
14k views

Is there a page listing all fields?

I am wondering whether somebody has compiled a list of fields which are available for creating content types in Drupal. There are many lists of the most popular modules, but it would be great to have ...
18
votes
10answers
24k 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
34k views

Check if a field is empty

How to you check that a field is empty, when you have the $node object?. I found this as possible solution: $field = field_get_items('node', $node, 'field_post_image'); I can check now: UPDATE.Ups!...
18
votes
3answers
15k views

How do I render nodes, now that node_view() has been deprecated?

In Drupal 7 I frequently use node_view() for rendering nodes (using view modes) in blocks or pages as follows: $nids = array(123,456,789); $nodes = node_load_multiple($nids); foreach ($nodes as $node)...
18
votes
4answers
5k views

How to hide “Manage Fields” and “Manage Display” tabs

I'm making a website with Drupal 7, and I was looking through the permissions for Taxonomies. I wanted to give the website maintainers a central place to manage the vocabularies and add terms, sort ...
17
votes
2answers
10k 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 ...
16
votes
2answers
11k views

How to debug EntityMalformedException?

I've got fatal error EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7700 of .\includes\common.inc) when trying to access user/xyz. I tried to ...