You are here

public function ContentTypeCourse::getAvailableContents in Opigno course 8

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

Get all the published entities in an array of LearningPathContent.

Return value

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

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides ContentTypeInterface::getAvailableContents

File

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

Class

ContentTypeCourse
Class ContentTypeCourse.

Namespace

Drupal\opigno_course\Plugin\OpignoGroupManagerContentType

Code

public function getAvailableContents() {
  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;
}