By j.b on
I'm am trying to update a node field programatically in d8 without saving the entire node.
Here's a sample of code:
function test_updattte($nid){
$entity = \Drupal\node\Entity\Node::load($nid);
$body_field = $entity->get('body');
$field_value = $body_field->getValue();
$field_value[0]['value'] = 'aafdfd';
$field_value[0]['summary'] = $field_value[0]['value'];
$body_field->setValue($field_value, TRUE);
}But the field value is not updated unless i save the entire entity.
In d7 there was the field_attach_update function.
Anyone can help ?
Comments
RE: D8 update node field programmatically.
This does not seem to be existing in D8 till now for updating you need to save the node !!
I've got the same problem as
I've got the same problem as well. Did you find a solution?
$node->save();
$entity->save();
Doesn't entity->save(), save
Doesn't entity->save(), save all fields of the the entity ?
What we want is to update/insert a specific field only without changing the changed timestamp of the entity.
I did not find a solution up to now
@HannahMR
I did not find a solution up to now and you ?
i've come across this page
http://www.drupalcontrib.org/api/drupal/drupal!core!modules!field!field....
Its says
Saves field data for an existing entity.
@deprecated as of Drupal 8.0. Use the entity system instead.
How do we use the entity system to achieve the same function as field_attach_update
The solution i've come up
The solution i've come up with is to do
First a delete on the concerned field to be inserted/updated respective table
then an insert
Sorry misread the question.
removed
Same problem here.
Same problem here.
I need to update a filed value (which need to contain a entity id) after a new entity created. I can do it in
hook_entity_insert()but I cannot save the entity again inside that hook. It will give me a error.D7 has a solution by
field_attach_updatefunction, but that function has been removed in D8.I believe that's because you
I believe that's because you don't do $entity->save() within the hook, you just alter the field value within the hook.
Thanks dkre. But $entity-
Thanks dkre. But
$entity->save()inside thathook_entity_insertwill cause a error like this:I think you can't use that
$entity->save()within another$entity->save()... How do you think?I think I fixed the error by
I think I fixed the error by set revision to false.
Setting field value for 'changed' results in unreliable behavior
Hello,
I hope I can add this to this question, otherwise I will create a new topic...
When doing exactly like OP does, but than setting the "changed" field like so:
$node->changed = "1500892843";But where the timestamp is variable per node (which are set correctly every time, because I checked the reported values every time).
Half of the time, the nodes get updated correctly (getting the timestamp values) and half of the time they just get the current_time value.
Can somebody please explain me why this is happening and how I can prevent this behavior?
EDIT: in the meanwhile, I stumbled upon this information on an external website. It describes this problem and says the presave function is the only way possible to (reliably) set the $node->changed value. In that same source, they question the inconsequent behavior between $node->created and $node->changed, which I think is a valid remark. So my question still stands... Why is this 50/50 correct instead of giving the same result (setting correct value all the time OR setting the false value all the time) with each update?
That's referring to D7,
That's referring to D7, right? we're trying to make D8 do this?
No I'm using D8?
No I'm using D8?
I came upon this because I
I came upon this because I wanted to have the "changed" value not get changed, as mentioned. If I set
$node->setChangedTime()to the original value, it would get ignored. Not sure where in the core code it is looking for those field diffs or even grabbing the changed time. But then if I set the changed time to something other than the original value, it worked. So, my code ended up like this:Not sure if a
$node->setNewRevision(FALSE)needs to be there again before the second save.I can't use save() method due
I can't use save() method due to the below reason
Can someone help here? Looking for alternative solution for field_attach_update
Well, you're trying to bypass
Well, you're trying to bypass all the APIs. It's rare someone will be able to support you on how to work around Drupal's APIs, people here explain how to work with the APIs. Generally, when you are trying put together a solution that bypasses Drupal, you'll need to do the work yourself, as others will not likely have done the same thing.
Contact me to contract me for D7 -> D10/11 migrations.
I want an alternative option
I want an alternative option for field_attach_update. I think this also bypasses all APIs.
Bypassing APIs
This is not a good idea. That said, if you're that keen then just write directly to the database. Why bother with using any of the APIs?