I have an entity called Entity Product. And this entity has a form, if you change the Title field in this form, Drupal will automatically save the new value of this field in the appropriate table in the database, but in addition, I also save the change of the Title field in the form to another database table product_product by creating a _custom_product_save_title
function. It is necessary. And this function is called in the hook_ENTITY_TYPE_update()
which tracks changes in Entity Product.
I need to add a check to see if the title is saved and if other fields are not saved. Please tell me what such a check should look like and where exactly should it be in the code?
function _custom_product_save_title($custom_product_id, $entity_product_title) { if (isset($fields['url']) && $fields['url'] == '') { if (isset($fields['name'])) { $fields['url'] = strtolower(str_replace(' ', '-', $fields['name'])); } } $id = $form_state->getValue('cid'); if (!empty($form_state->getValue('cid'))) { $query = $this->connection->update($this->getTableName()) ->condition('cid', $form_state->getValue('cid')); } else { $query = $this->connection->insert($this->getTableName()); } $result = $query ->fields($fields) ->execute(); if (!$id) { $id = $result; } Cache::invalidateTags([ "product:" . $form_state->getValue('cid'), ]); Cache::invalidateTags([ "product:$custom_product_id", ]); if (!$custom_product_id) { Cache::invalidateTags([ "product_list", ]); } } /** * Implements hook_ENTITY_TYPE_update(). */ function product_admin_node_update(DrupalCoreEntityEntityInterface $entity) { if ($entity->bundle() == 'product') { $custom_product_id = $entity->get('field_product_cid')->value; $entity_product_title = $entity->getTitle(); _custom_product_save_title($custom_product_id, $entity_product_title); } }
After debugging the code using xdebug, I see the following structure of what comes in the standard argument $entity
of the hook:
$entity fields field_product_cid field_second ..... title x-default list 0 values value = “My title”