Commit 267d0665 authored by Jack Over's avatar Jack Over
Browse files

Issue #3315760: Images are not loaded on the media add page

parent 9966c998
Loading
Loading
Loading
Loading
+0 −30
Original line number Diff line number Diff line
@@ -5,8 +5,6 @@
 * The module file.
 */

use Drupal\Core\Cache\CacheBackendInterface;

/**
 * Implements hook_theme().
 */
@@ -20,31 +18,3 @@ function cocoon_media_theme($existing, $type, $theme, $path) {

  return $variables;
}

/**
 * Get cached data.
 *
 * @param int $cid
 *   Cache id.
 * @param array $function_name
 *   Containing the object and the method.
 * @param array $params
 *   Parameters for the callback method.
 * @param int $expire_time
 *   Cache expire time.
 *
 * @return mixed|null
 *   The Cached data.
 */
function get_cached_data($cid, array $function_name, array $params = [], $expire_time = 0) {
  $data = NULL;
  if ($cache = \Drupal::cache()->get($cid)) {
    $data = $cache->data;
  }
  else {
    $data = call_user_func_array($function_name, $params);
    $expire_time = $expire_time === NULL ? CacheBackendInterface::CACHE_PERMANENT : time() + $expire_time;
    \Drupal::cache()->set($cid, $data, $expire_time);
  }
  return $data;
}
+35 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\cocoon_media;

use Drupal\Core\Cache\CacheBackendInterface;
use SoapClient;
use SoapHeader;
use StdClass;
@@ -171,6 +172,11 @@ class CocoonController {
   * @return \Drupal\cocoon_media\SoapFault|\Exception
   */
  public function getSets() {
    $cid = 'cocoon_media:get_sets';
    if ($cache = \Drupal::cache()->get($cid)) {
      return $cache->data;
    }

    try {
      $output = self::soapClient(
        $this->getRequestId(),
@@ -181,7 +187,8 @@ class CocoonController {
    catch (SoapFault $oSoapFault) {
      $output = $oSoapFault;
    }

    $expire_time = CacheBackendInterface::CACHE_PERMANENT;
    \Drupal::cache()->set($cid, $output, $expire_time, ['cocoon_media']);
    return $output;
  }

@@ -334,6 +341,33 @@ class CocoonController {
    return $output;
  }

  /**
   * Get cached data.
   *
   * @param int $cid
   *   Cache id.
   * @param array $function_name
   *   Containing the object and the method.
   * @param array $params
   *   Parameters for the callback method.
   * @param int $expire_time
   *   Cache expire time.
   *
   * @return mixed|null
   *   The Cached data.
   */
  function getCachedData($cid, array $function_name, array $params = [], int $expire_time = -1) {
    if ($cache = \Drupal::cache()->get($cid)) {
      $data = $cache->data;
    }
    else {
      $data = call_user_func_array($function_name, $params);
      $expire_time = $expire_time === -1 ? CacheBackendInterface::CACHE_PERMANENT : time() + $expire_time;
      \Drupal::cache()->set($cid, $data, $expire_time, ['cocoon_media']);
    }
    return $data;
  }

  /**
   * @param $errMsg
   *
+5 −5
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ class CMMController extends ControllerBase {
  protected $cocoonController;

  /**
   * {@inheritdoc}
   * The constructor.
   */
  public function __construct() {
    $this->settings = $this->config('cocoon_media.settings');
@@ -51,15 +51,15 @@ class CMMController extends ControllerBase {
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The response from cocoon in json.
   */
  public function getTagsAutocomplete(Request $req, $tag_name = '') {
  public function getTagsAutocomplete(Request $req, string $tag_name = '') {
    $params = $req->query->get('q');
    $tags_list = get_cached_data('cocoon_media:all_tags', [$this->cocoonController, 'getTags']);
    $tags_list = $this->cocoonController->getCachedData('cocoon_media:all_tags', [$this->cocoonController, 'getTags']);
    $tagNames = [];
    // Using autocomplete in forms does not work properly with paths,
    // I am adding this 'trick':
    // If tag_name is empty but parameter is not then us parameter.
    $tag_name = $tag_name ? $tag_name : $params;
    $filterd_tag_list = array_filter($tags_list, function ($item) use ($tag_name) {
    $tag_name = $tag_name ?: $params;
    $filterd_tag_list = array_filter($tags_list, static function ($item) use ($tag_name) {
      return strpos($item['name'], $tag_name) === 0;
    });

+23 −19
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\cocoon_media\Form;

use Drupal\cocoon_media\CocoonController;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\ConfigFormBase;
@@ -303,7 +305,7 @@ class CMMAddMediaForm extends ConfigFormBase {
        $media_bundle = 'file';
        $field_media_name = 'field_media_file';

        if (in_array($file_info['ext'], $this->fileTypeImage)) {
        if (in_array($file_info['ext'], $this->fileTypeImage, TRUE)) {
          // TODO replace with generic image bundle
          // or make configurable what the bundle is.
          $media_bundle = 'image';
@@ -313,7 +315,7 @@ class CMMAddMediaForm extends ConfigFormBase {
          $field_media_name = 'field_media_image';
        }

        if (in_array($file_info['ext'], $this->fileTypeVideo)) {
        if (in_array($file_info['ext'], $this->fileTypeVideo, TRUE)) {
          // TODO replace with generic video bundle
          // or make configurable what the bundle is.
          $media_bundle = 'video';
@@ -401,9 +403,6 @@ class CMMAddMediaForm extends ConfigFormBase {
    $filename = $prefix . $image_info['filename'] . '.' . $image_info['extension'];
    $public_path = 'public://cocoon_media_files/' . $filename;
    $local_path = $this->fileSystem->realpath($public_path);
    if (empty($filename)) {
      return '';
    }
    if (!in_array($image_info['extension'], $this->fileTypeImage, TRUE)) {
      return '';
    }
@@ -426,11 +425,10 @@ class CMMAddMediaForm extends ConfigFormBase {
   * @return array
   *   list of images.
   */
  public function getFilesByTag($tag_name) {
  public function getFilesByTag(string $tag_name): array {
    $tags_images_list = [];
    $tags_list = NULL;
    $matches = [];
    $tags_list = get_cached_data('cocoon_media:all_tags', [
    $tags_list = $this->cocoonController->getCachedData('cocoon_media:all_tags', [
      $this->cocoonController,
      'getTags',
    ], [], $this->cacheDuration);
@@ -442,7 +440,7 @@ class CMMAddMediaForm extends ConfigFormBase {

    if ($tag_name) {
      foreach ($tags_list as $tag) {
        $string_found = $tag_name ? strpos($tag['name'], $tag_name) : TRUE;
        $string_found = strpos($tag['name'], $tag_name);
        if ($string_found !== FALSE) {
          $matches[] = $tag['id'];
        }
@@ -450,7 +448,7 @@ class CMMAddMediaForm extends ConfigFormBase {
    }

    foreach ($matches as $tag_id) {
      $tag_files = get_cached_data('cocoon_media:tag_' . $tag_id, [
      $tag_files = $this->cocoonController->getCachedData('cocoon_media:tag_' . $tag_id, [
        $this->cocoonController,
        'getFilesByTag',
      ], [$tag_id], $this->cacheDuration);
@@ -550,8 +548,7 @@ class CMMAddMediaForm extends ConfigFormBase {
      . round($image_info['size'] / 1024, 2)
      . 'KB</p>',
    ];
    $rendered_item = \Drupal::service('renderer')->renderPlain($elm);
    return $rendered_item;
    return \Drupal::service('renderer')->renderPlain($elm);
  }

  /**
@@ -566,6 +563,12 @@ class CMMAddMediaForm extends ConfigFormBase {
   *   Options with rendered items.
   */
  public function buildOptionsElements($set_id, $tag_name) {
    $save_tag_name = Html::escape($tag_name);
    $cid = 'cocoon_media:options_file_list:' . $set_id . ':' . $save_tag_name;
    $cache = \Drupal::cache()->get($cid);
    if ($cache) {
      return $cache->data;
    }

    $file_list = $this->getImagesBySetId($set_id);
    if ($tag_name) {
@@ -575,18 +578,20 @@ class CMMAddMediaForm extends ConfigFormBase {
    $i = 0;
    foreach ($file_list as $image_info) {
      $i++;
      $rendered_item = get_cached_data('cocoon_media:option_item_' . $image_info['id'], [
      $rendered_item = $this->cocoonController->getCachedData('cocoon_media:option_item_' . $image_info['id'], [
        $this,
        'buildSingleOptionElement',
      ], [$image_info]);
      $options[$image_info['id']] = [
        'media_item' => $rendered_item,
      ];
      // TODO Make this nicer, now it only loads the first 150 items.
      if ($set_id === 'all' && $tag_name === '' && $i >= 25) {
      // TODO Make this nicer, now it only loads the first 30 items.
      if ($set_id === 'all' && $tag_name === '' && $i >= 30) {
        break;
      }
    }
    $expire_time = CacheBackendInterface::CACHE_PERMANENT;
    \Drupal::cache()->set($cid, $options, $expire_time, ['cocoon_media']);
    return $options;
  }

@@ -603,7 +608,7 @@ class CMMAddMediaForm extends ConfigFormBase {
    $images = [];

    if ($set_id !== 'all') {
      $results = get_cached_data('cocoon_media:set_' . $set_id, [
      $results = $this->cocoonController->getCachedData('cocoon_media:set_' . $set_id, [
        $this->cocoonController,
        'getFilesBySet',
      ], [$set_id], $this->cacheDuration);
@@ -681,11 +686,10 @@ class CMMAddMediaForm extends ConfigFormBase {
   *   Form state.
   *
   * @return mixed
   *   The cocooon media browser form.
   *   The cocoon media browser form.
   */
  public function refreshLibrary(array &$form, FormStateInterface &$form_state) {
    // TODO do we need to flush all caches?
    drupal_flush_all_caches();
    \Drupal::cache()->invalidateTags(['cocoon_media']);
    return $form['cocoon_media_browser'];
  }