You are here

public function ContentTypeCourse::getContent 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::getContent()

Get the entity as a LearningPathContent.

Parameters

int $entity_id: The entity ID.

Return value

\Drupal\opigno_group_manager\OpignoGroupContent|false The content loaded in a LearningPathContent. FALSE if not possible to load.

Overrides ContentTypeInterface::getContent

File

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

Class

ContentTypeCourse
Class ContentTypeCourse.

Namespace

Drupal\opigno_course\Plugin\OpignoGroupManagerContentType

Code

public function getContent($entity_id) {
  if (!is_numeric($entity_id)) {
    return FALSE;
  }

  /** @var \Drupal\group\Entity\Group $group */
  $group = Group::load($entity_id);
  if (empty($group)) {
    return FALSE;
  }
  if ($group
    ->getGroupType()
    ->id() != 'opigno_course') {
    return FALSE;
  }

  // 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'];
  }
  return new OpignoGroupContent($this
    ->getPluginId(), $this
    ->getEntityType(), $entity_id, $group
    ->label(), $img_url, $img_alt);
}