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

Fixes #4010 Remote Images for API Integrations #4019

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
"drupal/pathauto": "1.13.0",
"drupal/quick_node_clone": "1.17.0",
"drupal/redirect": "1.11.0",
"drupal/remote_stream_wrapper": "2.1.0",
"drupal/remote_stream_wrapper_widget": "1.4.0",
"drupal/role_delegation": "1.3.0",
"drupal/schema_metatag": "3.0.3",
"drupal/search_exclude": "3.0.0-beta1",
Expand Down Expand Up @@ -195,6 +197,9 @@
"drupal/role_delegation": {
"Restrict access to cancel/remove accounts with non-assignable roles (3299201)": "https://www.drupal.org/files/issues/2022-07-25/3299201-4.patch"
},
"drupal/remote_stream_wrapper": {
"Image styles setting extension cause access denied (3068898)": "https://www.drupal.org/files/issues/2024-12-02/3068898-12.patch"
},
"drupal/search_exclude": {
"Request for a Views Handler (2882683)": "https://www.drupal.org/files/issues/2023-06-29/request_for_a_views-2882683-14_0.patch"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dependencies:
- node
- path
- pathauto
- remote_stream_wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ source:
-
name: degrees
selector: 'Degrees'
-
name: image_url
label: 'Image URL'
selector: 'Person/photo_url'

process:
nid:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\az_person_profiles_import\EventSubscriber;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\migrate\Event\MigrateEvents;
Expand Down Expand Up @@ -82,6 +83,45 @@ public function onPostRowSave(MigratePostRowSaveEvent $event) {
if ($migration === 'az_person_profiles_import') {
$person = $this->entityTypeManager->getStorage('node')->load($id);
if (!empty($person)) {
$image = $event->getRow()->get('image_url');
// See if we have an image url.
// @todo move this functionality to process plugin.
if (!empty($image) && UrlHelper::isValid($image)) {
$fileStorage = $this->entityTypeManager->getStorage('file');
// Check if we already have a managed file for this Url.
$files = $fileStorage->loadByProperties(['uri' => $image]);
$file = reset($files);
if (!$file) {
$file = $fileStorage->create([
'uri' => $image,
]);
$file->save();
}
// Hook up file to media field or create new media entity.
if ($person->hasField('field_az_media_image')) {
/** @var \Drupal\media\MediaInterface $media */
$media = $person->field_az_media_image->entity;
// Media entity needs to be updated.
if ($media) {
// @todo cleanup this field access do be configured by process plugin.
// @phpstan-ignore-next-line
$media->field_media_az_image->target_id = $file->id();
$media->save();
}
else {
// Media doesn't exist yet, need to create it.
$media = $this->entityTypeManager->getStorage('media')->create([
'bundle' => 'az_image',
'field_media_az_image' => [
'target_id' => $file->id(),
],
]);
$media->save();
$person->field_az_media_image->target_id = $media->id();
$person->save();
}
}
}
$url = $person->toUrl()->toString();
$this->messenger->addMessage(t('Imported <a href="@link">@name</a>.', [
'@link' => $url,
Expand Down