You are here

class FacebookAlbumController in Facebook Album 8

Controller for Facebook Album.

Hierarchy

Expanded class hierarchy of FacebookAlbumController

File

src/Controller/FacebookAlbumController.php, line 18
Contains \Drupal\facebook_album\Controller\FacebookAlbumController.

Namespace

Drupal\facebook_album\Controller
View source
class FacebookAlbumController extends ControllerBase {

  /**
   * The FB Album controller.
   *
   * @var FacebookAlbumInterface
   */
  protected $facebook_album;

  /**
   * @param FacebookAlbumInterface $facebook_album
   *   The controls of facebook album.
   */
  public function __construct(FacebookAlbumInterface $facebook_album) {
    $this->facebook_album = $facebook_album;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('facebook_album.controller'));
  }

  /**
   * Fetch first set of albums specified in the settings menu
   *
   * @param \Drupal\block\BlockInterface $block
   * @return mixed
   */
  public function getAlbums(BlockInterface $block) {
    return $this
      ->getAlbumsNext($block);
  }

  /**
   * Fetch the next or previous set of cover photos from the specified page ID
   *
   * @param \Drupal\block\BlockInterface $block
   * @param null $after
   *  The id for fetching the next set of albums
   * @return \Zend\Diactoros\Response\JsonResponse
   *  A json object containing an html template and after id
   */
  public function getAlbumsNext(BlockInterface $block, $after = NULL) {
    $settings = $block
      ->get('settings');
    $limit = $settings['album_limit'];
    if ($limit < 1) {
      $limit = NULL;
    }
    else {

      // ensure that ID's can't be passed in to retrieve albums
      // if limit has been set to a non-zero number
      $after = NULL;
    }
    $path = $settings['page_id'] . '/albums';
    $parameters = [
      'after' => $after,
      'limit' => $limit,
      'fields' => 'location,description,name,cover_photo.fields(images)',
    ];
    $response = $this
      ->makeRequest($path, $parameters);

    // Filter out any albums from the config
    $filtered_content = $this
      ->filterAlbums($response['data'], $settings['albums'], $settings['album_visibility']);

    // Build json response
    $json_response = [];
    $render = [
      '#theme' => 'facebook_album_covers',
      '#settings' => $settings,
      '#photos' => $filtered_content,
    ];
    $json_response['data']['content'] = \Drupal::service('renderer')
      ->render($render);
    if (isset($response['paging']) && isset($response['paging']['next']) && $limit == NULL) {
      $json_response['data']['after'] = $response['paging']['cursors']['after'];
    }
    else {
      $json_response['data']['after'] = NULL;
    }
    return new JsonResponse($json_response);
  }

  /**
   * Fetch the first set of photos from the specified album
   *
   * @param \Drupal\block\BlockInterface $block
   * @param $album_id
   *  The album id to fetch photos from
   * @return mixed
   */
  public function getAlbum(BlockInterface $block, $album_id) {
    return $this
      ->getAlbumNext($block, $album_id);
  }

  /**
   * Fetch the next or previous set of photos from the specified album
   *
   * @param \Drupal\block\BlockInterface $block
   * @param $album_id
   *  The album id to fetch photos from
   * @param null $after
   *  The id for fetching the next or previous set of photos
   * @return \Zend\Diactoros\Response\JsonResponse
   */
  public function getAlbumNext(BlockInterface $block, $album_id, $after = NULL) {
    $settings = $block
      ->get('settings');
    $parameters = [
      'after' => $after,
      'fields' => 'url',
    ];
    $response = $this
      ->makeRequest($album_id . '/photos', $parameters);

    // Build json response
    $json_response = [];
    $render = [
      '#theme' => 'facebook_album_photos',
      '#settings' => $settings,
      '#photos' => $response['data'],
    ];
    $json_response['data']['content'] = \Drupal::service('renderer')
      ->render($render);
    $json_response['data']['photo_ids'] = $response['data'];
    if (isset($response['paging']) && isset($response['paging']['next'])) {
      $json_response['data']['after'] = $response['paging']['cursors']['after'];
    }
    return new JsonResponse($json_response);
  }

  /**
   * Fetch an individual photo url from a Facebook album photo
   *
   * @param $photo_id
   * @return \Zend\Diactoros\Response\JsonResponse
   */
  public function getPhoto($photo_id) {
    $json_response = array(
      'data' => NULL,
    );
    $parameters = [
      'fields' => 'images,name',
    ];
    $response = $this
      ->makeRequest($photo_id, $parameters);
    if (!isset($response['error'])) {
      $json_response['data']['url'] = $response['images'][0]['source'];
      $json_response['data']['name'] = isset($response['name']) ? $response['name'] : '';
    }
    return new JsonResponse($json_response);
  }

  /**
   * The Facebook API does not allow us to specify which albums to load or
   * exclude so after loading the albums we'll simply filter for any albums
   * we want to display
   *
   * @param $albums
   *    An array of albums from the facebook API
   * @param array $album_ids
   *    Album IDs used to determine a whitelist or blacklist of albums from
   * @param bool $include
   *    A flag, that if true, includes all albums specified in $albumIDs, otherwise
   *    it excludes all albums in $albumIDs
   * @return array
   *    An array of filtered albums
   */
  protected function filterAlbums($albums, $album_ids = [], $include = TRUE) {
    if (isset($album_ids[0]) && ($album_ids[0] != '' || $album_ids[0] == 0)) {
      $include = (bool) $include;
      $albums = array_filter($albums, function ($album) use ($album_ids, $include) {
        return $include === in_array($album['id'], $album_ids);
      });
    }
    return $albums;
  }

  /**
   * Makes and Caches responses to limit FB API traffic
   *
   * @param $path
   * @param array $parameters
   *
   * @return mixed
   */
  protected function makeRequest($path, $parameters = []) {

    // For consistency
    $cid = 'fba:' . str_replace("/", ":", $path);

    // Append params except non-data changers
    foreach ($parameters as $key => $parameter) {
      if ($key != 'fields' && !empty($parameter)) {
        $cid .= ':' . $parameter;
      }
    }

    // Check cache first before calling API
    if ($cache = \Drupal::cache()
      ->get($cid)) {
      $response = $cache->data;
    }
    else {
      $response = $this->facebook_album
        ->get($path, $parameters);
      if (!isset($response['error'])) {
        \Drupal::cache()
          ->set($cid, $response);
      }
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
FacebookAlbumController::$facebook_album protected property The FB Album controller.
FacebookAlbumController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
FacebookAlbumController::filterAlbums protected function The Facebook API does not allow us to specify which albums to load or exclude so after loading the albums we'll simply filter for any albums we want to display
FacebookAlbumController::getAlbum public function Fetch the first set of photos from the specified album
FacebookAlbumController::getAlbumNext public function Fetch the next or previous set of photos from the specified album
FacebookAlbumController::getAlbums public function Fetch first set of albums specified in the settings menu
FacebookAlbumController::getAlbumsNext public function Fetch the next or previous set of cover photos from the specified page ID
FacebookAlbumController::getPhoto public function Fetch an individual photo url from a Facebook album photo
FacebookAlbumController::makeRequest protected function Makes and Caches responses to limit FB API traffic
FacebookAlbumController::__construct public function
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.