You are here

public function ContentTypeCourse::getAllContents in Opigno course 3.x

Same name and namespace in other branches
  1. 8 src/Plugin/OpignoGroupManagerContentType/ContentTypeCourse.php \Drupal\opigno_course\Plugin\OpignoGroupManagerContentType\ContentTypeCourse::getAllContents()

Get all the entities in an array of LearningPathContent.

Return value

\Drupal\opigno_group_manager\OpignoGroupContent[]|false The contents or FALSE in case of error.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides ContentTypeInterface::getAllContents

File

src/Plugin/OpignoGroupManagerContentType/ContentTypeCourse.php, line 180

Class

ContentTypeCourse
Class ContentTypeCourse.

Namespace

Drupal\opigno_course\Plugin\OpignoGroupManagerContentType

Code

public function getAllContents() {
  try {

    /** @var \Drupal\group\Entity\Group[] $groups */
    $groups = \Drupal::entityTypeManager()
      ->getStorage('group')
      ->loadByProperties([
      'type' => 'opigno_course',
    ]);
  } catch (InvalidPluginDefinitionException $e) {

    // TODO: Log the error.
    return FALSE;
  }
  $contents = [];
  foreach ($groups as $group) {

    // Load image.
    $image = $this
      ->getCourseImage($group);
    if (!$image) {

      // If no image set, put default image.
      $img_url = $this
        ->getDefaultCourseImageUrl();
      $img_alt = 'Default course image';
    }
    else {

      // If there is an image set, get the URL of it.
      $image = $image
        ->getValue();
      $file = File::load($image[0]['target_id']);
      $uri = $file
        ->getFileUri();

      // I use this function `file_create_url` because all the others
      // methods ($file->toUrl(), URL::fromUri($uri)->toString(), ...)
      // doesn't work...
      $url = file_create_url($uri);
      $img_url = $url;
      $img_alt = $image[0]['alt'];
    }
    $contents[] = new OpignoGroupContent($this
      ->getPluginId(), $this
      ->getEntityType(), $group
      ->id(), $group
      ->label(), $img_url, $img_alt);
  }
  return $contents;
}