You are here

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

Try to get the content from a Request object.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\Drupal\opigno_group_manager\OpignoGroupContent|false The content if possible. FALSE otherwise.

Overrides ContentTypeInterface::getContentFromRequest

File

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

Class

ContentTypeCourse
Class ContentTypeCourse.

Namespace

Drupal\opigno_course\Plugin\OpignoGroupManagerContentType

Code

public function getContentFromRequest(Request $request) {
  $group = $request
    ->get('group', FALSE);
  if ($group === FALSE) {
    return FALSE;
  }

  // If the value is the group ID, load the group.
  if (is_numeric($group)) {

    /* @var \Drupal\group\Entity\Group $group */
    $group = Group::load($group);
  }
  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, $group
    ->id(), $group
    ->label(), $img_url, $img_alt);
}