Commit caf87134 authored by Nathaniel Burnett's avatar Nathaniel Burnett
Browse files

Issue #2930337 by Nathaniel: Continue working on dependency injection. Some cleanup.

parent 674413eb
Loading
Loading
Loading
Loading
+62 −15
Changes for src/Controller/PhotosAlbumController.php: 62 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,13 @@
namespace Drupal\photos\Controller;

use Drupal\Component\Utility\Html;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
@@ -22,6 +26,20 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 */
class PhotosAlbumController extends ControllerBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The entity manager.
   *
@@ -29,6 +47,20 @@ class PhotosAlbumController extends ControllerBase {
   */
  protected $entityManager;

  /**
   * The image factory.
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

  /**
   * The library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * The current request stack.
   *
@@ -48,16 +80,28 @@ class PhotosAlbumController extends ControllerBase {
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Image\ImageFactory $image_factory
   *   The image factory.
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The library discovery service.
   * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The current request stack.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, RequestStack $request_stack, RouteMatchInterface $route_match) {
  public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, DateFormatterInterface $date_formatter, EntityManagerInterface $entity_manager, ImageFactory $image_factory, LibraryDiscoveryInterface $library_discovery, RequestStack $request_stack, RouteMatchInterface $route_match) {
    $this->configFactory = $config_factory;
    $this->connection = $connection;
    $this->dateFormatter = $date_formatter;
    $this->entityManager = $entity_manager;
    $this->imageFactory = $image_factory;
    $this->libraryDiscovery = $library_discovery;
    $this->requestStack = $request_stack;
    $this->routeMatch = $route_match;
  }
@@ -68,7 +112,11 @@ class PhotosAlbumController extends ControllerBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('database'),
      $container->get('date.formatter'),
      $container->get('entity.manager'),
      $container->get('image.factory'),
      $container->get('library.discovery'),
      $container->get('request_stack'),
      $container->get('current_route_match')
    );
@@ -133,8 +181,7 @@ class PhotosAlbumController extends ControllerBase {
    $term = PhotosAlbum::orderValue($column, $sort, $limit, $order);
    // Album image's query.
    // @todo move to PhotosAlbum()->getImages().
    $db = \Drupal::database();
    $query = $db->select('file_managed', 'f')->extend('Drupal\Core\Database\Query\PagerSelectExtender');
    $query = $this->connection->select('file_managed', 'f')->extend('Drupal\Core\Database\Query\PagerSelectExtender');
    $query->join('photos_image', 'p', 'p.fid = f.fid');
    $query->join('users_field_data', 'u', 'u.uid = f.uid');
    $query->fields('f', ['fid'])
@@ -190,9 +237,9 @@ class PhotosAlbumController extends ControllerBase {
      if (!$photos_image_count && $image->count) {
        $image->links['count'] = $this->formatPlural($image->count, '@count visit', '@count visits', ['@count' => $image->count]);
      }
      $image->links['info'] = t('Uploaded on @time by @name', [
      $image->links['info'] = $this->t('Uploaded on @time by @name', [
        '@name' => $image->name,
        '@time' => \Drupal::service('date.formatter')->format($image->created, 'short'),
        '@time' => $this->dateFormatter->format($image->created, 'short'),
      ]);

      $image->class = [
@@ -205,7 +252,7 @@ class PhotosAlbumController extends ControllerBase {
      ];
      // Edit links.
      if ($edit) {
        $destination = drupal_get_destination();
        $destination = $this->getDestinationArray();
        $image->ajax['edit_url'] = $image->url . '/update';
        // Edit link.
        $url = Url::fromUri('base:photos/image/' . $image->fid . '/edit', [
@@ -218,7 +265,7 @@ class PhotosAlbumController extends ControllerBase {
            'class' => ['colorbox-load', 'photos-edit-edit'],
          ],
        ]);
        $image->ajax['edit_link'] = Link::fromTextAndUrl(t('Edit'), $url);
        $image->ajax['edit_link'] = Link::fromTextAndUrl($this->t('Edit'), $url);

        // Jeditable inline editing integration.
        // @todo add setting to enable Jeditable?
@@ -230,7 +277,7 @@ class PhotosAlbumController extends ControllerBase {
          'des_edit' => ' id="photos-image-edit-des-' . $image->fid . '"',
          'title_edit' => ' id="photos-image-edit-title-' . $image->fid . '"',
        ];
        $jeditable_library = \Drupal::service('library.discovery')->getLibraryByName('photos', 'photos.jeditable');
        $jeditable_library = $this->libraryDiscovery->getLibraryByName('photos', 'photos.jeditable');

        // Link to update album cover.
        if (!isset($node->album['cover']) || isset($node->album['cover']['fid']) && $node->album['cover']['fid'] <> $image->fid) {
@@ -239,13 +286,13 @@ class PhotosAlbumController extends ControllerBase {
            'file' => $image->fid,
            'destination' => $destination['destination'],
          ]);
          $image->links['cover'] = Link::fromTextAndUrl(t('Set to Cover'), $url);
          $image->links['cover'] = Link::fromTextAndUrl($this->t('Set to Cover'), $url);
        }
      }
      $image->ajax['del_id'] = '';
      if ($del) {
        $image->ajax['del_id'] = 'id="photos_ajax_del_' . $image->fid . '"';
        $destination = drupal_get_destination();
        $destination = $this->getDestinationArray();
        // Delete link.
        $url = Url::fromUri('base:photos/image/' . $image->fid . '/delete', [
          'query' => [
@@ -255,7 +302,7 @@ class PhotosAlbumController extends ControllerBase {
            'class' => ['colorbox-load', 'photos-edit-delete'],
          ],
        ]);
        $image->ajax['del_link'] = Link::fromTextAndUrl(t('Delete'), $url);
        $image->ajax['del_link'] = Link::fromTextAndUrl($this->t('Delete'), $url);
      }

      // Build image view for album.
@@ -277,11 +324,11 @@ class PhotosAlbumController extends ControllerBase {
      $album['access']['edit'] = $edit;
      // Node edit link.
      $url = Url::fromUri('base:node/' . $nid . '/edit');
      $album['node_edit_url'] = \Drupal::l(t('Album settings'), $url);
      $album['node_edit_url'] = Link::fromTextAndUrl($this->t('Album settings'), $url);

      // Image management link.
      $url = Url::fromUri('base:node/' . $nid . '/photos');
      $album['image_management_url'] = \Drupal::l(t('Upload photos'), $url);
      $album['image_management_url'] = Link::fromTextAndUrl($this->t('Upload photos'), $url);

      // Album URL.
      $album['album_url'] = Url::fromUri('base:photos/album/' . $nid)->toString();
@@ -295,7 +342,7 @@ class PhotosAlbumController extends ControllerBase {
        }
        else {
          // @todo is this needed?
          $image_info = \Drupal::service('image.factory')->get($node->album['cover']['uri']);
          $image_info = $this->imageFactory->get($node->album['cover']['uri']);
          $title = $node->getTitle();
          $album_cover_array = [
            '#theme' => 'image_style',
@@ -338,7 +385,7 @@ class PhotosAlbumController extends ControllerBase {
    }
    else {
      $content = [
        '#markup' => t('Album is empty'),
        '#markup' => $this->t('Album is empty'),
        '#cache' => [
          'tags' => [
            'photos:album:' . $nid,
+1 −1
Changes for src/Controller/PhotosAlbumsRecentController.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -89,7 +89,7 @@ class PhotosAlbumsRecentController extends ControllerBase {
      $output .= $this->renderer->render($pager);
    }
    else {
      $output .= t('No albums have been created yet.');
      $output .= $this->t('No albums have been created yet.');
    }

    return [
+3 −3
Changes for src/Controller/PhotosController.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -100,14 +100,14 @@ class PhotosController extends ControllerBase {
        $more_link = [
          '#type' => 'more_link',
          '#url' => $url,
          '#title' => t('View more'),
          '#title' => $this->t('View more'),
        ];
        $content .= $this->renderer->render($more_link);
      }
      if ($type == 'user') {
        return [
          'content' => $content,
          'title' => t("@name's albums", ['@name' => $album[0]['node']->name]),
          'title' => $this->t("@name's albums", ['@name' => $album[0]['node']->name]),
        ];
      }
      else {
@@ -137,7 +137,7 @@ class PhotosController extends ControllerBase {
    return [
      '#theme' => 'photos_default',
      '#content' => $content,
      '#empty' => t('No photos available.'),
      '#empty' => $this->t('No photos available.'),
    ];
  }

+51 −19
Changes for src/Controller/PhotosEditController.php: 51 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -7,7 +7,10 @@ use Drupal\Core\Url;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
@@ -24,6 +27,13 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 */
class PhotosEditController extends ControllerBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The current path.
   *
@@ -38,6 +48,20 @@ class PhotosEditController extends ControllerBase {
   */
  protected $entityManager;

  /**
   * The FormBuilder object.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The renderer.
   *
@@ -62,10 +86,16 @@ class PhotosEditController extends ControllerBase {
  /**
   * Constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
@@ -73,9 +103,12 @@ class PhotosEditController extends ControllerBase {
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(CurrentPathStack $current_path, EntityManagerInterface $entity_manager, RendererInterface $renderer, RequestStack $request_stack, RouteMatchInterface $route_match) {
  public function __construct(Connection $connection, CurrentPathStack $current_path, EntityManagerInterface $entity_manager, FormBuilderInterface $form_builder, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RequestStack $request_stack, RouteMatchInterface $route_match) {
    $this->connection = $connection;
    $this->currentPath = $current_path;
    $this->entityManager = $entity_manager;
    $this->formBuilder = $form_builder;
    $this->moduleHandler = $module_handler;
    $this->renderer = $renderer;
    $this->requestStack = $request_stack;
    $this->routeMatch = $route_match;
@@ -86,8 +119,11 @@ class PhotosEditController extends ControllerBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('database'),
      $container->get('path.current'),
      $container->get('entity.manager'),
      $container->get('form_builder'),
      $container->get('module_handler'),
      $container->get('renderer'),
      $container->get('request_stack'),
      $container->get('current_route_match')
@@ -147,8 +183,7 @@ class PhotosEditController extends ControllerBase {
   */
  public function editImage($file) {
    $fid = $file;
    $db = \Drupal::database();
    $query = $db->select('file_managed', 'f');
    $query = $this->connection->select('file_managed', 'f');
    $query->join('photos_image', 'p', 'p.fid = f.fid');
    $query->join('users_field_data', 'u', 'f.uid = u.uid');
    $query->fields('f', ['uri', 'filemime', 'created', 'filename', 'filesize']);
@@ -158,7 +193,7 @@ class PhotosEditController extends ControllerBase {
    $image = $query->execute()->fetchObject();

    if ($image && isset($image->fid)) {
      $edit_form = \Drupal::formBuilder()->getForm('\Drupal\photos\Form\PhotosImageEditForm', $image);
      $edit_form = $this->formBuilder->getForm('\Drupal\photos\Form\PhotosImageEditForm', $image);
      return $edit_form;
    }
    else {
@@ -172,15 +207,15 @@ class PhotosEditController extends ControllerBase {
   */
  public function deleteImage($file) {
    $fid = $file;
    if (\Drupal::moduleHandler()->moduleExists('colorbox')) {
    $confirm_delete_form = $this->formBuilder->getForm('\Drupal\photos\Form\PhotosImageConfirmDeleteForm', $fid);
    if ($this->moduleHandler->moduleExists('colorbox_load')) {
      // Dispaly form in modal popup.
      // @todo does this still work?
      $confirm_delete_form = \Drupal::formBuilder()->getForm('\Drupal\photos\Form\PhotosImageConfirmDeleteForm', $fid);
      // @todo does this still work? Test colorbox_load module.
      print $this->renderer->render($confirm_delete_form);
    }
    else {
      // Render full page.
      return \Drupal::formBuilder()->getForm('\Drupal\photos\Form\PhotosImageConfirmDeleteForm', $fid);
      return $confirm_delete_form;
    }
  }

@@ -207,10 +242,9 @@ class PhotosEditController extends ControllerBase {
      // Check user image edit permissions.
      // @todo photos.routing.yml _csrf_token: 'TRUE'.
      if ($fid && _photos_access('imageEdit', $fid)) {
        $db = \Drupal::database();
        switch ($switch) {
          case 'title':
            $db->update('photos_image')
            $this->connection->update('photos_image')
              ->fields([
                'title' => $value,
              ])
@@ -220,7 +254,7 @@ class PhotosEditController extends ControllerBase {
            break;

          case 'des':
            $db->update('photos_image')
            $this->connection->update('photos_image')
              ->fields([
                'des' => $value,
              ])
@@ -230,7 +264,7 @@ class PhotosEditController extends ControllerBase {
            break;
        }
        // Clear cache.
        $pid = $db->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
        $pid = $this->connection->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
        if ($pid) {
          Cache::invalidateTags(['node:' . $pid, 'photos:album:' . $pid]);
        }
@@ -265,20 +299,19 @@ class PhotosEditController extends ControllerBase {
      // Check user image edit permissions.
      // @todo photos.routing.yml _csrf_token: 'TRUE'.
      if ($fid && _photos_access('imageEdit', $fid)) {
        $db = \Drupal::database();
        switch ($switch) {
          case 'title':
            $value = $db->query("SELECT title FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
            $value = $this->connection->query("SELECT title FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
            $message = $value;
            break;

          case 'des':
            $value = $db->query("SELECT des FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
            $value = $this->connection->query("SELECT des FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
            $message = $value;
            break;
        }
        // Clear cache.
        $pid = $db->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
        $pid = $this->connection->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [':fid' => $fid])->fetchField();
        if ($pid) {
          Cache::invalidateTags(['node:' . $pid, 'photos:album:' . $pid]);
        }
@@ -297,8 +330,7 @@ class PhotosEditController extends ControllerBase {
   * Set album cover.
   */
  public function setAlbumCover($node, $file) {
    $db = \Drupal::database();
    $pid = $db->query('SELECT pid FROM {photos_image} WHERE fid = :fid', [':fid' => $file])->fetchField();
    $pid = $this->connection->query('SELECT pid FROM {photos_image} WHERE fid = :fid', [':fid' => $file])->fetchField();
    if ($pid == $node) {
      $album = new PhotosAlbum($pid);
      $album->setCover($file);
@@ -308,7 +340,7 @@ class PhotosEditController extends ControllerBase {
      $response = new RedirectResponse($goto);
      $response->send();
      return [
        '#markup' => t('Cover successfully set.'),
        '#markup' => $this->t('Cover successfully set.'),
      ];
    }
    else {
+18 −6
Changes for src/Controller/PhotosImageController.php: 18 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\photos\Controller;

use Drupal\Component\Utility\Html;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
@@ -35,6 +36,13 @@ class PhotosImageController extends ControllerBase {
   */
  protected $entityManager;

  /**
   * The library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * The current route match.
   *
@@ -51,13 +59,16 @@ class PhotosImageController extends ControllerBase {
   *   The database connection.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The library discovery service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, EntityManagerInterface $entity_manager, RouteMatchInterface $route_match) {
  public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, EntityManagerInterface $entity_manager, LibraryDiscoveryInterface $library_discovery, RouteMatchInterface $route_match) {
    $this->configFactory = $config_factory;
    $this->connection = $connection;
    $this->entityManager = $entity_manager;
    $this->libraryDiscovery = $library_discovery;
    $this->routeMatch = $route_match;
  }

@@ -69,6 +80,7 @@ class PhotosImageController extends ControllerBase {
      $container->get('config.factory'),
      $container->get('database'),
      $container->get('entity.manager'),
      $container->get('library.discovery'),
      $container->get('current_route_match')
    );
  }
@@ -124,12 +136,12 @@ class PhotosImageController extends ControllerBase {
    if (_photos_access('imageEdit', $node)) {
      $image->ajax['edit_url'] = Url::fromUri('base:photos/image/' . $image->fid . '/update')->toString();
      // Set album cover.
      $image->links['cover'] = Link::createFromRoute(t('Set to Cover'), 'photos.album.update.cover', [
      $image->links['cover'] = Link::createFromRoute($this->t('Set to Cover'), 'photos.album.update.cover', [
        'node' => $image->pid,
        'file' => $fid,
      ],
      [
        'query' => drupal_get_destination(),
        'query' => $this->getDestinationArray(),
      ]);
    }
    $image->class = [
@@ -151,7 +163,7 @@ class PhotosImageController extends ControllerBase {
          'class' => ['colorbox-load', 'photos-edit-edit'],
        ],
      ]);
      $image->ajax['edit_link'] = Link::fromTextAndUrl(t('Edit'), $url);
      $image->ajax['edit_link'] = Link::fromTextAndUrl($this->t('Edit'), $url);

      $image->class = [
        'title_class' => ' jQueryeditable_edit_title',
@@ -161,7 +173,7 @@ class PhotosImageController extends ControllerBase {
        'des_edit' => ' id="photos-image-edit-des-' . $image->fid . '"',
        'title_edit' => ' id="photos-image-edit-title-' . $image->fid . '"',
      ];
      $jeditable_library = \Drupal::service('library.discovery')->getLibraryByName('photos', 'photos.jeditable');
      $jeditable_library = $this->libraryDiscovery->getLibraryByName('photos', 'photos.jeditable');
    }
    if (_photos_access('imageDelete', $node)) {
      // Image delete link.
@@ -174,7 +186,7 @@ class PhotosImageController extends ControllerBase {
          'class' => ['colorbox-load', 'photos-edit-delete'],
        ],
      ]);
      $image->ajax['del_link'] = Link::fromTextAndUrl(t('Delete'), $url);
      $image->ajax['del_link'] = Link::fromTextAndUrl($this->t('Delete'), $url);
    }
    if ($config->get('photos_comment')) {
      // Comment integration.
Loading