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

Fix multi-file media to allow image fields only on image derivatives. #892

Merged
merged 3 commits into from
Dec 14, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Drupal\islandora\Plugin\Action\AbstractGenerateDerivativeMediaFile;

/**
* Emits a Node for generating derivatives event.
* Emits a Media for generating derivatives event.
*
* Attaches the result as a file in an image field on the emitting
* Media ("multi-file media").
*
* @Action(
* id = "generate_image_derivative_file",
Expand All @@ -24,7 +27,6 @@ public function defaultConfiguration() {
$config['path'] = '[date:custom:Y]-[date:custom:m]/[media:mid]-ImageService.jpg';
$config['mimetype'] = 'application/xml';
$config['queue'] = 'islandora-connector-houdini';
$config['destination_media_type'] = 'file';
$config['scheme'] = $this->config->get('default_scheme');
return $config;
}
Expand All @@ -34,9 +36,30 @@ public function defaultConfiguration() {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['mimetype']['#description'] = $this->t('Mimetype to convert to (e.g. application/xml, etc...)');
$map = $this->entityFieldManager->getFieldMapByFieldType('image');
$file_fields = $map['media'];
$file_options = array_combine(array_keys($file_fields), array_keys($file_fields));
$file_options = array_merge(['' => ''], $file_options);
// @todo figure out how to write to thumbnail, which is not a real field.
// see https://github.com/Islandora/islandora/issues/891.
unset($file_options['thumbnail']);

$form['destination_field_name'] = [
'#required' => TRUE,
'#type' => 'select',
'#options' => $file_options,
'#title' => $this->t('Destination Image field'),
'#default_value' => $this->configuration['destination_field_name'],
'#description' => $this->t('This Action stores the derivative in an
Image field. If you need to store the result in a File field, use
"Generate a Derivative File for Media Attachment". Selected target field
must be an additional field, not the media\'s main storage field.
Selected target field must be present on the media.'),
];

$form['mimetype']['#value'] = 'image/jpeg';
$form['mimetype']['#type'] = 'hidden';
$form['mimetype']['#description'] = 'Mimetype to convert to. Must be
compatible with the destination image field.';
return $form;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Controller/MediaSourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ public function attachToMedia(
*/
public function attachToMediaAccess(AccountInterface $account, RouteMatch $route_match) {
$media = $route_match->getParameter('media');
$node = $this->utils->getParentNode($media);
return AccessResult::allowedIf($node->access('update', $account) && $account->hasPermission('create media'));
return AccessResult::allowedIf($media->access('update', $account));
}

}
4 changes: 2 additions & 2 deletions src/MediaSource/MediaSourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public function putToNode(
'uri' => $content_location,
'filename' => $this->fileSystem->basename($content_location),
'filemime' => $mimetype,
'status' => FILE_STATUS_PERMANENT,
]);
$file->setPermanent();

// Validate file extension.
$source_field_config = $this->entityTypeManager->getStorage('field_config')->load("media.$bundle.$source_field");
Expand Down Expand Up @@ -349,8 +349,8 @@ public function putToMedia(
'uri' => $content_location,
'filename' => $this->fileSystem->basename($content_location),
'filemime' => $mimetype,
'status' => FILE_STATUS_PERMANENT,
]);
$file->setPermanent();

// Validate file extension.
$bundle = $media->bundle();
Expand Down
17 changes: 14 additions & 3 deletions src/Plugin/Action/AbstractGenerateDerivativeMediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Drupal\Core\Url;

/**
* Emits a Node for generating derivatives event.
* Emits a Media for generating derivatives event.
*
* Attaches the result as a file in a file field on the emitting
* Media ("multi-file media").
*
* @Action(
* id = "generate_derivative_file",
Expand Down Expand Up @@ -98,15 +101,23 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$image_options = array_combine(array_keys($image_fields), array_keys($image_fields));

$file_options = array_merge(['' => ''], $file_options, $image_options);

// @todo figure out how to write to thumbnail, which is not a real field.
// see https://github.com/Islandora/islandora/issues/891.
unset($file_options['thumbnail']);

$form['event']['#disabled'] = 'disabled';

$form['destination_field_name'] = [
'#required' => TRUE,
'#type' => 'select',
'#options' => $file_options,
'#title' => $this->t('Destination File field Name'),
'#title' => $this->t('Destination File field'),
'#default_value' => $this->configuration['destination_field_name'],
'#description' => $this->t('File field on Media Type to hold derivative. Cannot be the same as source'),
'#description' => $this->t('This Action stores a derivative file
in a File or Image field on a media. The destination field
must be an additional field, not the media\'s main storage field.
Selected destination field must be present on the media.'),
];

$form['args'] = [
Expand Down