Commit 9966c998 authored by Jack Over's avatar Jack Over Committed by Jack Over
Browse files

Issue #3315759 by scuba_fly: Fix errors on media add page in drupal 9

parent 38a21bef
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -320,11 +320,12 @@ class CocoonController {
   */
  public function getVersion() {
    try {
      $output = self::soapClient(
      $client = self::soapClient(
        $this->getRequestId(),
        $this->subdomain,
        $this->username,
        $this->secretkey)->getVersion();
        $this->secretkey);
      $output = $client->getVersion();
    }
    catch (SoapFault $oSoapFault) {
      $output = $oSoapFault;
+34 −20
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\cocoon_media\Form;

use Drupal\cocoon_media\CocoonController;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
@@ -46,12 +47,13 @@ class CMMAddMediaForm extends ConfigFormBase {
   *
   * @var array
   */
  protected $fileTypeImage = [
  protected array $fileTypeImage = [
    'jpg',
    'jpeg',
    'png',
    'gif',
    'tiff',
    'tif',
    'bmp',
  ];

@@ -60,7 +62,7 @@ class CMMAddMediaForm extends ConfigFormBase {
   *
   * @var array
   */
  protected $fileTypeVideo = [
  protected array $fileTypeVideo = [
    'mp4',
    'avi',
    'flv',
@@ -86,11 +88,17 @@ class CMMAddMediaForm extends ConfigFormBase {
   */
  protected $fileSystem;

  /**
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * {@inheritdoc}
   */
  public function __construct(FileSystemInterface $file_system) {
  public function __construct(FileSystemInterface $file_system, FileUrlGeneratorInterface $url_generator) {
    $this->fileSystem = $file_system;
    $this->urlGenerator = $url_generator;
    $this->config = $this->config('cocoon_media.settings');
    $this->cocoonController = new CocoonController(
      $this->config->get('cocoon_media.domain'),
@@ -104,7 +112,8 @@ class CMMAddMediaForm extends ConfigFormBase {

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('file_system')
      $container->get('file_system'),
      $container->get('file_url_generator')
     );
  }

@@ -190,11 +199,14 @@ class CMMAddMediaForm extends ConfigFormBase {
          }
        }
      }

      $paging_size = $this->config->get('cocoon_media.paging_size') ?? 15;
      $options = $this->buildOptionsElements($set, $tag_name);
      $options_chunk = array_chunk($options, (int) $this->config->get('cocoon_media.paging_size'), TRUE);
      $total_pages = count($options_chunk);
      $current_page = $current_page < 0 ? 0 : $current_page;
      $options_chunk = array_chunk($options, $paging_size, TRUE);
      $total_pages = count($options_chunk) ?? 0;
      $current_page = max($current_page, 0);
      $current_page = $current_page >= $total_pages ? $total_pages - 1 : $current_page;

      $form['cocoon_media_browser']['tag_elements'] = [
        '#prefix' => '<div class="container-inline">',
        '#suffix' => '</div>',
@@ -387,21 +399,22 @@ class CMMAddMediaForm extends ConfigFormBase {
   */
  private function remoteThumbToLocal(array $image_info, $prefix) {
    $filename = $prefix . $image_info['filename'] . '.' . $image_info['extension'];
    $local_path = 'public://cocoon_media_files/' . $filename;

    $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 '';
    }
    if (!file_exists($local_path)) {
      $thumb_info = $this->cocoonController->getThumbInfo($image_info['id']);
      if (empty($thumb_info['web'])) {
        return '';
      }
      if (!empty($thumb_info['web'])) {
      $this->retrieveRemoteFile($thumb_info['web'], $local_path);
    }
    }
    return $local_path;
    return $public_path;
  }

  /**
@@ -497,14 +510,15 @@ class CMMAddMediaForm extends ConfigFormBase {
   *   The rendered HTML.
   */
  public function buildSingleOptionElement(array $image_info) {
    global $base_url;
    $thumb_url = $base_url . '/' . drupal_get_path('module', 'cocoon_media')

    $thumb_url = '/' . drupal_get_path('module', 'cocoon_media')
      . '/images/generic.png';
    $image_size = 100;
    $thumb = $this->remoteThumbToLocal($image_info, 'thumb_');
    if (!empty($thumb)) {
      $thumb_url = file_create_url($thumb);
      $thumb_url = $this->urlGenerator->generateString($thumb);
      $image_size = getimagesize(urldecode($this->fileSystem->realpath($thumb)))[1] ?? 100;
    }
    $image_size = getimagesize($thumb_url);

    $elm = [
      '#type' => 'fieldset',
@@ -520,7 +534,7 @@ class CMMAddMediaForm extends ConfigFormBase {
      '#title' => '&nbsp;',
      '#attributes' => [
        'class' => 'media-thumb',
        'style' => "background-image:url(" . $thumb_url . "); background-repeat: no-repeat;background-size: contain;width:100%;height:" . $image_size[1] . "px;max-height:150px",
        'style' => "background-image:url(" . $thumb_url . "); background-repeat: no-repeat;background-size: contain;width:100%;height:" . $image_size . "px;max-height:150px",
      ],
    ];
    $elm['title'] = [
@@ -559,7 +573,7 @@ class CMMAddMediaForm extends ConfigFormBase {
    }
    $options = [];
    $i = 0;
    foreach ($file_list as $idx => $image_info) {
    foreach ($file_list as $image_info) {
      $i++;
      $rendered_item = get_cached_data('cocoon_media:option_item_' . $image_info['id'], [
        $this,
@@ -569,7 +583,7 @@ class CMMAddMediaForm extends ConfigFormBase {
        'media_item' => $rendered_item,
      ];
      // TODO Make this nicer, now it only loads the first 150 items.
      if ($set_id === 'all' && $tag_name === '' && $i >= 150) {
      if ($set_id === 'all' && $tag_name === '' && $i >= 25) {
        break;
      }
    }
+8 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ class CMMSettingsForm extends ConfigFormBase {
      '#title' => $this->t('Cocoon API key'),
      '#default_value' => $config->get('cocoon_media.api_key'),
      '#description' => $this->t('Register on <a target="_blank" href="https://use-cocoon.nl/">use-cocoon.nl</a> and get your API key.'),
      '#required' => TRUE,
    ];
    // CMM domain.
    $form['cocoon_media_settings']['domain'] = [
@@ -52,6 +53,7 @@ class CMMSettingsForm extends ConfigFormBase {
      '#title' => $this->t('Cocoon Domain'),
      '#default_value' => $config->get('cocoon_media.domain'),
      '#description' => $this->t('Your Cocoon domain (is the first part of the url of your cocoon site)'),
      '#required' => TRUE,
    ];
    // CMM username.
    $form['cocoon_media_settings']['username'] = [
@@ -59,6 +61,7 @@ class CMMSettingsForm extends ConfigFormBase {
      '#title' => $this->t('Cocoon Username'),
      '#default_value' => $config->get('cocoon_media.username'),
      '#description' => $this->t('Your Cocoon Username'),
      '#required' => TRUE,
    ];
    $form['cocoon_media_settings']['media_image_bundle'] = [
      '#type' => 'textfield',
@@ -76,15 +79,17 @@ class CMMSettingsForm extends ConfigFormBase {
    $form['cocoon_media_settings']['paging_size'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Pager size'),
      '#default_value' => $config->get('cocoon_media.paging_size', 15),
      '#default_value' => $config->get('cocoon_media.paging_size') ?? 15,
      '#description' => $this->t('How many items per page'),
      '#required' => TRUE,
    ];
    // CMM cache duration in seconds.
    $form['cocoon_media_settings']['cache_duration'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Cache duration'),
      '#default_value' => $config->get('cocoon_media.cache_duration', 60 * 5),
      '#default_value' => $config->get('cocoon_media.cache_duration') ?? 60 * 5,
      '#description' => $this->t('How long cached data will last (in seconds).'),
      '#required' => TRUE,
    ];

    if (
@@ -152,7 +157,7 @@ class CMMSettingsForm extends ConfigFormBase {
      $config->get('cocoon_media.username'),
      $config->get('cocoon_media.api_key'));
    $version = $cocoonController->getVersion();
    $output = '<b>Curren API version is: ' . $version . '</b>';
    $output = '<b>Current API version is: ' . $version . '</b>';

    return [
      '#markup' => $output,