Questions tagged [entities]

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

Filter by
Sorted by
Tagged with
88 votes
7 answers
18k views

When is it appropriate to create an entity instead of 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 ...
revolt's user avatar
  • 1,131
80 votes
11 answers
228k 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?
dbj44's user avatar
  • 4,734
78 votes
4 answers
74k 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 ( ...
Justin's user avatar
  • 3,703
57 votes
9 answers
53k 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 ...
NRaf's user avatar
  • 853
53 votes
6 answers
100k 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 ...
SiliconMind's user avatar
  • 3,151
52 votes
3 answers
11k 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 ...
Merrick's user avatar
  • 1,134
50 votes
7 answers
45k 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 ...
radu c's user avatar
  • 4,350
49 votes
1 answer
44k 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 ...
Sugandh Khanna's user avatar
49 votes
3 answers
30k 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 ...
artfulrobot's user avatar
  • 3,989
46 votes
15 answers
45k 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 ...
artfulrobot's user avatar
  • 3,989
40 votes
11 answers
29k 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 ...
irishbuzz's user avatar
  • 645
38 votes
7 answers
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 ...
Clive's user avatar
  • 166k
36 votes
12 answers
38k 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 ...
nocksock's user avatar
  • 689
36 votes
9 answers
6k 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. ...
rafamd's user avatar
  • 513
35 votes
5 answers
18k 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?
daphshez's user avatar
  • 1,082
32 votes
4 answers
57k 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 ...
user avatar
32 votes
4 answers
35k 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)...
batigolix's user avatar
  • 2,584
31 votes
3 answers
13k views

Differences between value and safe_value

What are the differences between ['value'] and ['safe_value']?
Citricguy's user avatar
  • 7,904
30 votes
5 answers
28k 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['...
Shafiul's user avatar
  • 1,226
29 votes
6 answers
16k 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?
Letharion's user avatar
  • 27.5k
27 votes
3 answers
39k 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 ...
Andrew's user avatar
  • 711
27 votes
3 answers
19k views

How to list all available entity types?

How to list all available entity types in Drupal using drush? Related: How to get list of all node types?
kenorb's user avatar
  • 14.1k
27 votes
5 answers
21k 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 ...
googletorp's user avatar
  • 35.4k
27 votes
1 answer
21k 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 ...
vfclists's user avatar
  • 1,269
25 votes
3 answers
15k 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 ...
Torrance's user avatar
  • 531
25 votes
6 answers
27k 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('...
Vinodkumar SC's user avatar
24 votes
10 answers
34k 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 ...
user379468's user avatar
  • 2,520
24 votes
5 answers
19k 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->...
Smos's user avatar
  • 647
23 votes
3 answers
28k views

Get all fields defined in a bundle

In Drupal 7, to get all fields used in a content type, I use the following code. $fields = field_info_instances('node', 'article'); What is the equivalent for Drupal 8?
Pravin Ajaaz's user avatar
  • 3,532
23 votes
6 answers
31k views

How to get bundle label from entity

How do I get the human-readable label of a bundle from a loaded entity. Lets say I want to get the label of a node of bundle my_article which is "My awesome article" // Load a node $node = Drupal::...
Linus's user avatar
  • 1,297
23 votes
5 answers
43k 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;...
hpn's user avatar
  • 2,398
23 votes
4 answers
1k 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, ...
Jon Freed's user avatar
  • 1,051
23 votes
1 answer
10k 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 ...
Clive's user avatar
  • 166k
22 votes
4 answers
47k views

Perform a query with an entity field condition with multiple values

I have a content type that has a entity reference field that allows users to add multiple taxonomy terms in that field. I'm attempting to perform queries that grab nodes that have a specific set of ...
Amy's user avatar
  • 5,632
22 votes
8 answers
20k 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 (...
Nick Tomlin's user avatar
21 votes
5 answers
40k 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="...
Ben's user avatar
  • 313
21 votes
5 answers
6k 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 ...
SMTF's user avatar
  • 946
21 votes
2 answers
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 ...
Chapabu's user avatar
  • 13.8k
21 votes
4 answers
23k 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 ...
Fedir RYKHTIK's user avatar
20 votes
4 answers
17k 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 ...
Punchy's user avatar
  • 359
20 votes
7 answers
15k 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 ...
Eloar's user avatar
  • 303
20 votes
4 answers
12k 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 ...
joachim's user avatar
  • 846
20 votes
2 answers
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. ...
amateur barista's user avatar
20 votes
4 answers
15k 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 ...
ctlarsen's user avatar
  • 255
20 votes
4 answers
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 ...
starsinmypockets's user avatar
19 votes
5 answers
26k views

How do I get the node type label from a node object?

I'm trying to get the bundle label (node type label) of the node from a node object in Drupal 8. This works, but seems long-winded: $node_type = \Drupal::entityManager()->getStorage('node_type')-&...
oknate's user avatar
  • 14.1k
19 votes
3 answers
16k 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 ...
Kojo's user avatar
  • 3,523
19 votes
3 answers
15k 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 ...
Joren's user avatar
  • 2,440
18 votes
4 answers
10k 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.
j2r's user avatar
  • 981
18 votes
9 answers
12k views

How can I remove items from the allowed values list of a select field that has data for the values?

I've created a content type that has a list / select option field, and I've entered the key|value pairs as necessary for the select list to function. Data has been entred, and it's been decided that ...
blue928's user avatar
  • 3,804

1
2 3 4 5
175