The node type property is now a string field on the entity. When #1757452: Support config entities in entity reference fields is committed, change this to an entity reference field for easier access to the node type entity.

CommentFileSizeAuthor
#8 2039607-8.patch3.05 KBclaudiu.cristea
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

claudiu.cristea’s picture

Sure we want this? Because getting all article nids was (so) straight. $nids = db_query("SELECT nid FROM {node} WHERE type = :type", array(':type' => 'article'))->fetchCol() :(

tstoeckler’s picture

In the new world order, that's what EntityFieldQuery is for:
Something like

$nids = Drupal::entityQuery('node')->condition('type', $type)->execute();

should do the trick. In D8 you will almost never write a direct query anyway, unless you're writing a storage backend.

Edit: Apparently, EFQ -> EQ

claudiu.cristea’s picture

This means that type will go out from {node} (plus {node_field_data}, {node_field_revision}) and will be stored, as body, in his own table?

More:

  1. The field widget should be hidden (locked) by default in node forms?
  2. Every new node bundle will automatically get an instance of Entity Reference field?
amateescu’s picture

I hope what Xano meant was to use the entity reference field that came with the new Entity API (Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem) not the configurable field provided by the Entity reference module (Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase), which means that it will still be stored in the base tables of the node entity.

Xano’s picture

Yes, that's what I meant. I'm sorry for the confusion. It's easy to mix those two things up.

claudiu.cristea’s picture

Ok, ok, that sounds good. I was tempted to take this task but I got no other similar example in core and, as I'm not 100% familiar with this, I don't know exactly were to start :)

Xano’s picture

A similar situation would be the node uid field, which is also an entity reference. Thing is if you make a patch now, it will most certainly break, because #1757452: Support config entities in entity reference fields isn't in yet. You can, however, start working on it, so we have a non-test usage of the patch in that issue.

claudiu.cristea’s picture

Assigned: Unassigned » claudiu.cristea
Status: Active » Needs review
FileSize
3.05 KB

I think this is what we want here... or I'm missing something.

With this patch, the node type entity can be reached in this way:


$node->get('type')->entity;

$node->getNodeType();

amateescu’s picture

Yep, that's exactly what we need :) This probably needs to be extended to all content entity types, and I would prefer to do it in a single issue/patch since it's such a small change.

claudiu.cristea’s picture

From IRC:

Xano: claudiu_cristea: Then I'd add a setter to the interface as well

It seems to me a read-only property but let's debate this. Opinions?

Status: Needs review » Needs work

The last submitted patch, 2039607-8.patch, failed testing.

claudiu.cristea’s picture

Unfortunately it breaks Condition tests. What I found is that validate fails in \Drupal\Core\Plugin\ContextAwarePluginBase::setContextValue() . It seems that, somewhere, the validation still tries to check $node->type[0]['target_id'] for node type value. It should check $node->type.

EDIT: And this seems something changed by the fact that type is now entity_reference_field.

claudiu.cristea’s picture

Assigned: claudiu.cristea » Unassigned

Think somebody else has to take this.

klausi’s picture

klausi’s picture

Status: Needs work » Fixed

This has been committed already in #2112239: Convert base field and property definitions, yay!

xjm’s picture

Category: Feature request » Task
Status: Fixed » Closed (duplicate)