Skip to main content

Alter entity value before save

How to list the name of Entity types in site to use hooks,

use Drupal\Core\Entity\EntityInterface;

function hook_entity_presave(EntityInterface $entity) {

}

or
function hook_entity_type_presave(EntityInterface $entity) {

}
but you should know the exact name of entity type , to do so
use : drupal debug:entity (drush command)
so for taxonomy case use,
function itg_multi_category_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
  if(!empty($entity->bundle()) && $entity->bundle() == 'category_management'){
    $entity_values = $entity->toArray();
    //Get the custom parent cat id 
    $parent_custom = $entity_values['field_relation'][0]['target_id'];
    if($parent_custom == ''){
      $parent_custom = 0;
    }
    $term_load = \Drupal\taxonomy\Entity\Term::load($entity_values['tid'][0]['value']);
    $term_load->set('parent',$parent_custom);
    $term_load->save();
    }
}

Comments