class FacebookAlbumController in Facebook Album 8
Controller for Facebook Album.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\facebook_album\Controller\FacebookAlbumController
Expanded class hierarchy of FacebookAlbumController
File
- src/
Controller/ FacebookAlbumController.php, line 18 - Contains \Drupal\facebook_album\Controller\FacebookAlbumController.
Namespace
Drupal\facebook_album\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
FacebookAlbumController:: |
protected | property | The FB Album controller. | |
FacebookAlbumController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
FacebookAlbumController:: |
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:: |
public | function | Fetch the first set of photos from the specified album | |
FacebookAlbumController:: |
public | function | Fetch the next or previous set of photos from the specified album | |
FacebookAlbumController:: |
public | function | Fetch first set of albums specified in the settings menu | |
FacebookAlbumController:: |
public | function | Fetch the next or previous set of cover photos from the specified page ID | |
FacebookAlbumController:: |
public | function | Fetch an individual photo url from a Facebook album photo | |
FacebookAlbumController:: |
protected | function | Makes and Caches responses to limit FB API traffic | |
FacebookAlbumController:: |
public | function | ||
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |