Loading cocoon_media.module +0 −30 Original line number Diff line number Diff line Loading @@ -5,8 +5,6 @@ * The module file. */ use Drupal\Core\Cache\CacheBackendInterface; /** * Implements hook_theme(). */ Loading @@ -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; } src/CocoonController.php +35 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\cocoon_media; use Drupal\Core\Cache\CacheBackendInterface; use SoapClient; use SoapHeader; use StdClass; Loading Loading @@ -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(), Loading @@ -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; } Loading Loading @@ -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 * Loading src/Controller/CMMController.php +5 −5 Original line number Diff line number Diff line Loading @@ -29,7 +29,7 @@ class CMMController extends ControllerBase { protected $cocoonController; /** * {@inheritdoc} * The constructor. */ public function __construct() { $this->settings = $this->config('cocoon_media.settings'); Loading @@ -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; }); Loading src/Form/CMMAddMediaForm.php +23 −19 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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'; Loading @@ -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'; Loading Loading @@ -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 ''; } Loading @@ -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); Loading @@ -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']; } Loading @@ -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); Loading Loading @@ -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); } /** Loading @@ -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) { Loading @@ -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; } Loading @@ -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); Loading Loading @@ -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']; } Loading Loading
cocoon_media.module +0 −30 Original line number Diff line number Diff line Loading @@ -5,8 +5,6 @@ * The module file. */ use Drupal\Core\Cache\CacheBackendInterface; /** * Implements hook_theme(). */ Loading @@ -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; }
src/CocoonController.php +35 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\cocoon_media; use Drupal\Core\Cache\CacheBackendInterface; use SoapClient; use SoapHeader; use StdClass; Loading Loading @@ -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(), Loading @@ -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; } Loading Loading @@ -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 * Loading
src/Controller/CMMController.php +5 −5 Original line number Diff line number Diff line Loading @@ -29,7 +29,7 @@ class CMMController extends ControllerBase { protected $cocoonController; /** * {@inheritdoc} * The constructor. */ public function __construct() { $this->settings = $this->config('cocoon_media.settings'); Loading @@ -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; }); Loading
src/Form/CMMAddMediaForm.php +23 −19 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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'; Loading @@ -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'; Loading Loading @@ -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 ''; } Loading @@ -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); Loading @@ -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']; } Loading @@ -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); Loading Loading @@ -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); } /** Loading @@ -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) { Loading @@ -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; } Loading @@ -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); Loading Loading @@ -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']; } Loading