1

I would like to have all shared albums of a certain google account automatically into a Drupal website. I was able to establish a connection with 'Google_photos_api' and show all shared albums on site with code below.

<?php
$GooglePhotosService = \Drupal::service('google_photos_api.client');
try {
  $optParams=array("pageSize" => 50);
  $SharedAlbumlijst = $GooglePhotosService->googleServicePhotosLibrary->sharedAlbums->listSharedAlbums($optParams);  
  ksm($SharedAlbumlijst);  
  echo '<div id="GooglePhotos" class="grid views-view-grid">';
  echo '<div class="row">';  
  foreach ($SharedAlbumlijst as $album) {
    echo '<div class="col-6 col-sm-6 col-md-4 col-lg-4 col-xl-3" style="margin-bottom: 10px;">';    
    // Get some properties of an album
    $albumId = $album->getId();
    $title = $album->getTitle();
    $productUrl = $album->getProductUrl();
    $coverPhotoBaseUrl = $album->getCoverPhotoBaseUrl() . '=w200-h200-c';
    echo '<h5>' . $title . '</h5>';
    echo '<a href="' . $coverPhotoBaseUrl . '" id="' . $albumId . '">';
    echo '<img src="' . $coverPhotoBaseUrl . '" alt="' . $title . '">';
    echo '</a>';
    echo '</div>';
  }
  echo '</div>';
}
catch (Exception $e) {
  ksm($e);
}

The problem I cannot figure out is to list all media-items of a given album. Code below is not functioning. I don't know how to pass the albumID into this??

<?php
$GooglePhotosService = \Drupal::service('google_photos_api.client');

try {
  $albumId = 'ANfpKL0unQw8nWACRpVCPj0JxmZ5JHkX_xCecI-VPFxKrFScPPfpE8HakFWvKG3KFVXNnNyigXB0';
  $optParams = ['albumId' => $albumId];
  $mediaItems = $GooglePhotosService->googleServicePhotosLibrary->mediaItems->search($optParams);

  ksm($MediaItems);
}

catch (Exception $e) {
  ksm($e);
}

Any help would be great!! Thanks in advance!!

Error code:
The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Google_Service_PhotosLibrary_Resource_MediaItems::search() must be an instance of Google_Service_PhotosLibrary_SearchMediaItemsRequest, array given, called in C:\Xampp2\htdocs\Demertr\modules\contrib\php\php.module(81) : eval()'d code on line 7 in Google_Service_PhotosLibrary_Resource_MediaItems->search() (line 79 of vendor\google\apiclient-services\src\Google\Service\PhotosLibrary\Resource\MediaItems.php).

MediaItems.php

class Google_Service_PhotosLibrary_Resource_MediaItems extends Google_Service_Resource {

public function search(Google_Service_PhotosLibrary_SearchMediaItemsRequest $postBody, $optParams = []) {
    $params = ['postBody' => $postBody];
    $params = array_merge($params, $optParams);
    return $this->call('search', [$params], 'Google_Service_PhotosLibrary_SearchMediaItemsResponse');
  }
}
SearchMediaItemsRequest.php

class Google_Service_PhotosLibrary_SearchMediaItemsRequest extends Google_Model {
  public $albumId;
  protected $filtersType = 'Google_Service_PhotosLibrary_Filters';
  protected $filtersDataType = '';
  public $pageSize;
  public $pageToken;
...
1
  • Hi and welcome to Drupal Answers! This question might get more attention and better answers on stackoverflow.com, as it's not directly related to Drupal.
    – berliner
    Dec 27 '21 at 22:27
0

No idea really, but looking at the error message and the docs at https://google.github.io/php-photoslibrary/v1.1.0/classes/Google.Photos.Library.V1.SearchMediaItemsRequest.html (if that is the right version), I would say that you need to create an instance of SearchMediaItemsRequest, set the album id on that object and then pass that into the search method. So maybe something like this based on your code:

$media_request = new SearchMediaItemsRequest();
$media_request->setAlbumId($albumId);
$mediaItems = $GooglePhotosService->googleServicePhotosLibrary->mediaItems->search($media_request);
3
  • Dear Berliner, Thanks for your reaction, but I still cannot figure this out. I tried your suggestion, but then I'm getting another error: Error: Class 'SearchMediaItemsRequest' not found in eval() (line 11 of modules\contrib\php\php.module(81) : eval()'d code).
    – Jan Moris
    Dec 28 '21 at 20:09
  • Don't do this kind of stuff with the help of the PHP module. Like ever! Really, don't! It's bad! Take the time instead to learn how Drupal works, then create a custom module and do it there. There are plenty of tutorials that will help you do this properly.
    – berliner
    Dec 28 '21 at 20:22
  • I run this code in my own module, only for testing I use the php module. But thanks anyway.
    – Jan Moris
    Dec 28 '21 at 20:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.