You are here

function tft_entity_presave in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \tft_entity_presave()

Implements entity_presave().

File

./tft.module, line 533
Contains tft.module.

Code

function tft_entity_presave(EntityInterface $entity) {

  // Create folder for new learning path.
  if ($entity
    ->getEntityTypeId() === 'group' && $entity
    ->bundle() === 'learning_path' && $entity
    ->isNew()) {

    /** @var \Drupal\group\Entity\Group $entity */
    if ($entity
      ->get('field_learning_path_folder')
      ->isEmpty()) {
      $folder = Term::create([
        'vid' => 'tft_tree',
        'name' => $entity
          ->label(),
        'parent' => 0,
      ]);
      $folder
        ->save();
      $entity
        ->set('field_learning_path_folder', [
        'target_id' => $folder
          ->id(),
      ]);
    }
  }

  // Update folder for new learning path.
  if ($entity
    ->getEntityTypeId() === 'group' && $entity
    ->bundle() === 'learning_path' && !$entity
    ->isNew()) {
    $folder = $entity
      ->get('field_learning_path_folder')->target_id;
    $folder = Term::load($folder);
    if ($folder instanceof TermInterface) {
      $new_name = $entity
        ->label();
      $folder
        ->setName($new_name);
      $folder
        ->save();
    }
  }
}