Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecations + rector fixes #4

Open
wants to merge 2 commits into
base: 1.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bynder_local.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Local Bynder Assets
description: 'Stores image assets in an image field'
type: module
package: Media
core_version_requirement: ^8.7.7 || ^9
core_version_requirement: ^8.7.7 || ^9 || ^10
configure: bynder.configuration_form

dependencies:
Expand Down
49 changes: 28 additions & 21 deletions bynder_local.module
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Utility\Error;
use Drupal\bynder\Plugin\Field\FieldType\BynderMetadataItem;
use Drupal\bynder\Plugin\media\Source\Bynder;
use Drupal\Component\Serialization\Json;
Expand All @@ -26,16 +28,18 @@ function bynder_local_media_presave(MediaInterface $media) {
$image_url = bynder_local_get_image_url($item);

if (!$image_url) {
\Drupal::logger('bynder_local')->warning('Missing asset for media @label (@id)', ['@label' => $media->label(), '@id' => $media->id()]);
Drupal::logger('bynder_local')->warning('Missing asset for media @label (@id)', ['@label' => $media->label(), '@id' => $media->id()]);
return;
}

$field_definition = $media->getFieldDefinition('field_bynder_image');
$location = $field_definition->getFieldStorageDefinition()->getSetting('uri_scheme') . '://' . $field_definition->getSetting('file_directory');
$location = \Drupal::token()->replace($location);
$location = Drupal::token()->replace($location);

\Drupal::service('file_system')->prepareDirectory($location, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if ($file = system_retrieve_file($image_url, $location, TRUE)) {
Drupal::service('file_system')->prepareDirectory($location, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if ($data = (string) \Drupal::httpClient()->get($image_url)->getBody()) {
$destination = $location . '/' . basename(parse_url($image_url, PHP_URL_PATH));
$file = Drupal::service('file.repository')->writeData($data, $destination, FileSystemInterface::EXISTS_REPLACE);
$media->set('field_bynder_image', $file);
}
}
Expand All @@ -55,30 +59,33 @@ function bynder_local_bynder_media_update_alter(MediaInterface $media, array $it

$image_url = bynder_local_get_image_url($item);
if (!$image_url) {
\Drupal::logger('bynder_local')->warning('Missing asset for media @label (@id)', ['@label' => $media->label(), '@id' => $media->id()]);
Drupal::logger('bynder_local')->warning('Missing asset for media @label (@id)', ['@label' => $media->label(), '@id' => $media->id()]);
return;
}

// Check the asset date, if it changed after the local file or if there is
// no local file, fetch it again.
if ($media->get('field_bynder_image')->entity) {
try {
$modified = new \DateTime($item['dateModified']);
$modified = new DateTime($item['dateModified']);
if ($modified->getTimestamp() < $media->get('field_bynder_image')->entity->getCreatedTime()) {
return;
}
}
catch (\Exception $e) {
watchdog_exception('bynder_local', $e);
catch (Exception $e) {
Drupal::logger('bynder_local')->warning('Missing asset for media @label (@id)', ['@label' => $media->label(), '@id' => $media->id()]);
DeprecationHelper::backwardsCompatibleCall(Drupal::VERSION, '10.1.0', fn() => Error::logException(Drupal::logger('bynder_local'), $e), fn() => watchdog_exception('bynder_local', $e));
}
}

$field_definition = $media->getFieldDefinition('field_bynder_image');
$location = $field_definition->getFieldStorageDefinition()->getSetting('uri_scheme') . '://' . $field_definition->getSetting('file_directory');
$location = \Drupal::token()->replace($location);
$location = Drupal::token()->replace($location);

\Drupal::service('file_system')->prepareDirectory($location, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if ($file = system_retrieve_file($image_url, $location, TRUE)) {
Drupal::service('file_system')->prepareDirectory($location, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if ($data = (string) \Drupal::httpClient()->get($image_url)->getBody()) {
$destination = $location . '/' . basename(parse_url($image_url, PHP_URL_PATH));
$file = Drupal::service('file.repository')->writeData($data, $destination, FileSystemInterface::EXISTS_REPLACE);

// If there already is a local file, mark it as temporary.
if ($media->get('field_bynder_image')->entity) {
Expand All @@ -103,19 +110,19 @@ function bynder_local_bynder_media_update_alter(MediaInterface $media, array $it
* The image URL or NULL if it can't be fetched.
*/
function bynder_local_get_image_url(array $item) {
$config = \Drupal::config('bynder_local.settings');
$config = Drupal::config('bynder_local.settings');
if ($config->get('derivative') == 'original') {
return $item['original'] ?? NULL;
}
elseif ($config->get('derivative') == '_download') {
/** @var \Drupal\bynder\BynderApi $api */
$api = \Drupal::service('bynder_api');
$api = Drupal::service('bynder_api');
try {
$response = $api->getAssetBankManager()->getMediaDownloadLocation($item['id'])->wait();
return $response['s3_file'];
}
catch (\Exception $e) {
watchdog_exception('bynder_local', $e);
catch (Exception $e) {
DeprecationHelper::backwardsCompatibleCall(Drupal::VERSION, '10.1.0', fn() => Error::logException(Drupal::logger('bynder_local'), $e), fn() => watchdog_exception('bynder_local', $e));
}
}
else {
Expand All @@ -126,14 +133,14 @@ function bynder_local_get_image_url(array $item) {
/**
* Implements hook_form_FORM_ID_alter().
*/
function bynder_local_form_bynder_configuration_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
function bynder_local_form_bynder_configuration_form_alter(&$form, FormStateInterface $form_state) {

$field_map = \Drupal::service('entity_field.manager')->getFieldMap();
$field_map = Drupal::service('entity_field.manager')->getFieldMap();

try {
$derivatives = array_merge(array_map(function ($item) {
return $item['prefix'];
}, \Drupal::service('bynder_api')->getDerivatives()),
}, Drupal::service('bynder_api')->getDerivatives()),
['mini', 'webimage', 'thul', 'original']
);
$derivatives = array_combine($derivatives, $derivatives);
Expand All @@ -154,15 +161,15 @@ function bynder_local_form_bynder_configuration_form_alter(&$form, \Drupal\Core\
}
else {
$labels = [];
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
$bundles = Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
foreach ($field_map['media']['field_bynder_image']['bundles'] as $bundle) {
$labels[] = $bundles[$bundle]['label'];
}

$form['bynder_local']['#description'] = t('The following media types are storing a local asset: @labels.', ['@labels' => implode(', ', $labels)]);
}

$config = \Drupal::configFactory()->getEditable('bynder_local.settings');
$config = Drupal::configFactory()->getEditable('bynder_local.settings');

$form['bynder_local']['local_derivative'] = [
'#type' => 'select',
Expand All @@ -179,7 +186,7 @@ function bynder_local_form_bynder_configuration_form_alter(&$form, \Drupal\Core\
* Form submit callback.
*/
function bynder_local_settings_submit($form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('bynder_local.settings');
$config = Drupal::configFactory()->getEditable('bynder_local.settings');
$config->set('derivative', $form_state->getValue('local_derivative'));
$config->save();
}