private function getClass(array $class) {
    $nodeStorage = $this->entityTypeManager->getStorage('node');

    // Try to get existing activity.
    $existingActivities = $nodeStorage->getQuery()
      ->condition('title', $class['category'])
      ->condition('type', 'activity')
      ->condition('field_activity_category', $this->programSubcategory)
      ->execute();

    if (!$existingActivities) {
      // No activities found. Create one.
      $activity = Node::create([
        'uid' => 1,
        'lang' => 'und',
        'type' => 'activity',
        'title' => $class['category'],
        'field_activity_description' => [
          [
            'value' => $class['description'],
            'format' => 'full_html',
          ],
        ],
        'field_activity_category' => [['target_id' => $this->programSubcategory]],
      ]);

      $activity->save();
    }
    else {
      // Use the first found existing one.
      $activityId = reset($existingActivities);
      $activity = $nodeStorage->load($activityId);
    }

    // Try to find class.
    $existingClasses = $nodeStorage->getQuery()
      ->condition('title', $class['title'])
      ->condition('field_class_activity', $activity->id())
      ->condition('field_class_description', $class['description'])
      ->execute();

    if (!empty($existingClasses)) {
      $classId = reset($existingClasses);
      $class = $nodeStorage->load($classId);
    }
    else {
      $paragraphs = [];
      foreach (['class_sessions', 'branches_popup_class'] as $type) {
        $paragraph = Paragraph::create(['type' => $type]);
        $paragraph->isNew();
        $paragraph->save();
        $paragraphs[] = [
          'target_id' => $paragraph->id(),
          'target_revision_id' => $paragraph->getRevisionId(),
        ];
      }
      $class = Node::create([
        'uid' => 1,
        'lang' => 'und',
        'type' => 'class',
        'title' => $class['title'],
        'field_class_description' => [
          [
            'value' => $class['description'],
            'format' => 'full_html',
          ],
        ],
        'field_class_activity' => [
          [
            'target_id' => $activity->id(),
          ],
        ],
        'field_content' => $paragraphs,
      ]);

      $class->save();
    }

    return [
      'target_id' => $class->id(),
      'target_revision_id' => $class->getRevisionId(),
    ];
  }
private function getClass(array $class) {
    $nodeStorage = $this->entityTypeManager->getStorage('node');

    // Try to get existing activity.
    $existingActivities = $nodeStorage->getQuery()
      ->condition('title', $class['category'])
      ->condition('type', 'activity')
      ->condition('field_activity_category', $this->programSubcategory)
      ->execute();

    if (!$existingActivities) {
      // No activities found. Create one.
      $activity = Node::create([
        'uid' => 1,
        'lang' => 'und',
        'type' => 'activity',
        'title' => $class['category'],
        'field_activity_description' => [
          [
            'value' => $class['description'],
            'format' => 'full_html',
          ],
        ],
        'field_activity_category' => [['target_id' => $this->programSubcategory]],
      ]);

      $activity->save();
    }
    else {
      // Use the first found existing one.
      $activityId = reset($existingActivities);
      $activity = $nodeStorage->load($activityId);
    }

    // Try to find class.
    $existingClasses = $nodeStorage->getQuery()
      ->condition('title', $class['title'])
      ->condition('field_class_activity', $activity->id())
      ->condition('field_class_description', $class['description'])
      ->execute();

    if (!empty($existingClasses)) {
      $classId = reset($existingClasses);
      $class = $nodeStorage->load($classId);
    }
    else {
      $paragraphs = [];
      foreach (['class_sessions', 'branches_popup_class'] as $type) {
        $paragraph = Paragraph::create(['type' => $type]);
        $paragraph->isNew();
        $paragraph->save();
        $paragraphs[] = [
          'target_id' => $paragraph->id(),
          'target_revision_id' => $paragraph->getRevisionId(),
        ];
      }
      $class = Node::create([
        'uid' => 1,
        'lang' => 'und',
        'type' => 'class',
        'title' => $class['title'],
        'field_class_description' => [
          [
            'value' => $class['description'],
            'format' => 'full_html',
          ],
        ],
        'field_class_activity' => [
          [
            'target_id' => $activity->id(),
          ],
        ],
        'field_content' => $paragraphs,
      ]);

      $class->save();
    }

    return [
      'target_id' => $class->id(),
      'target_revision_id' => $class->getRevisionId(),
    ];
  }
private function getClass ( array $ class ) { $ nodeStorage = $ this -> entityTypeManager -> getStorage ( 'node' ) ; // Try to get existing activity. $ existingActivities = $ nodeStorage -> getQuery ( ) -> condition ( 'title' , $ class [ 'category' ] ) -> condition ( 'type' , 'activity' ) -> condition ( 'field_activity_category' , $ this -> programSubcategory ) -> execute ( ) ; if ( ! $ existingActivities ) { // No activities found. Create one. $ activity = Node :: create ( [ 'uid' => 1 , 'lang' => 'und' , 'type' => 'activity' , 'title' => $ class [ 'category' ] , 'field_activity_description' => [ [ 'value' => $ class [ 'description' ] , 'format' => 'full_html' , ] , ] , 'field_activity_category' => [ [ 'target_id' => $ this -> programSubcategory ] ] , ] ) ; $ activity -> save ( ) ; } else { // Use the first found existing one. $ activityId = reset ( $ existingActivities ) ; $ activity = $ nodeStorage -> load ( $ activityId ) ; } // Try to find class. $ existingClasses = $ nodeStorage -> getQuery ( ) -> condition ( 'title' , $ class [ 'title' ] ) -> condition ( 'field_class_activity' , $ activity -> id ( ) ) -> condition ( 'field_class_description' , $ class [ 'description' ] ) -> execute ( ) ; if ( ! empty ( $ existingClasses ) ) { $ classId = reset ( $ existingClasses ) ; $ class = $ nodeStorage -> load ( $ classId ) ; } else { $ paragraphs = [ ] ; foreach ( [ 'class_sessions' , 'branches_popup_class' ] as $ type ) { $ paragraph = Paragraph :: create ( [ 'type' => $ type ] ) ; $ paragraph -> isNew ( ) ; $ paragraph -> save ( ) ; $ paragraphs [ ] = [ 'target_id' => $ paragraph -> id ( ) , 'target_revision_id' => $ paragraph -> getRevisionId ( ) , ] ; } $ class = Node :: create ( [ 'uid' => 1 , 'lang' => 'und' , 'type' => 'class' , 'title' => $ class [ 'title' ] , 'field_class_description' => [ [ 'value' => $ class [ 'description' ] , 'format' => 'full_html' , ] , ] , 'field_class_activity' => [ [ 'target_id' => $ activity -> id ( ) , ] , ] , 'field_content' => $ paragraphs , ] ) ; $ class -> save ( ) ; } return [ 'target_id' => $ class -> id ( ) , 'target_revision_id' => $ class -> getRevisionId ( ) , ] ; }
Create class or use existing.

@param array $class
Class data.

@return array
Class references.

@throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
@throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
@throws \Drupal\Core\Entity\EntityStorageException
Create class or use existing .