Score:-1

How to make a check that my entity title field is written to needed database table and that no other field is written there?

my flag

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(\Drupal\Core\Entity\EntityInterface $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”
apaderno avatar
us flag
Welcome to Drupal Answers! When `hook_ENTITY_TYPE_update()` is invoked, the entiiy and its fields are already saved in the database.
leymannx avatar
ne flag
Use `hook_ENTITY_TYPE_presave` to override data before it gets stored without needing to mess with the database or cache. Leave all that to Drupal.
apaderno avatar
us flag
`_custom_product_save_title()` doesn't have any `$form_state` or `$fields` parameter nor (as function) does it have access to `$this`. This means that `$form_state->getValue('cid')` and `$this->connection->update($this->getTableName())` would cause errors or warnings.
apaderno avatar
us flag
When asking a question, please show the code you are really using and verify the code is valid PHP code.
sonfd avatar
in flag
I think your actual question is "[How] to add a check to see if the title is saved and if other fields are not saved [in hook_entity_update()]", but I'm not sure what that means. My best guess is that you want to test if the entity had its title changed, but no other changes, but that doesn't make much sense to me in the context of your overall post.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.