Skip to content

Commit

Permalink
8.2.15
Browse files Browse the repository at this point in the history
- Provide a message on media form and lock default field media items (#92) (d7610b7)
  • Loading branch information
pookmish authored Apr 9, 2021
2 parents 47579fa + f14f7e2 commit 9922eeb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Stanford Media

8.x-2.15
--------------------------------------------------------------------------------
_Release Date: 2021-04-09_

- Provide a message on media form and lock default field media items (#92) (d7610b7)

8.x-2.14
--------------------------------------------------------------------------------
_Release Date: 2021-03-05_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Media Duplicate Validation'
type: module
description: 'Media Validation plugins to help prevent duplication of media items'
core_version_requirement: ^8.8 || ^9
version: 8.x-2.14
version: 8.x-2.15
package: media
dependencies:
- drupal:media
2 changes: 1 addition & 1 deletion stanford_media.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Provides media module configuration and plugins.
core_version_requirement: ^8.8 || ^9
package: media
type: module
version: 8.x-2.14
version: 8.x-2.15
dependencies:
- dropzonejs:dropzonejs
- drupal:breakpoint
Expand Down
78 changes: 75 additions & 3 deletions stanford_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* stanford_media.module
*/

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\editor\Entity\Editor;
use Drupal\media\MediaInterface;
use Drupal\stanford_media\Form\MediaLibraryEmbeddableForm;
use Drupal\stanford_media\Form\MediaLibraryFileUploadForm;
use Drupal\stanford_media\Form\MediaLibraryGoogleFormForm;
use Drupal\stanford_media\Form\MediaLibraryEmbeddableForm;

/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
Expand Down Expand Up @@ -109,8 +112,12 @@ function stanford_media_preprocess_media(&$variables) {
if ($media->getSource()->getPluginId() != 'file') {
return;
}
$media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($media->bundle());
$source_field = $media->getSource()->getSourceFieldDefinition($media_type)->getName();
$media_type = \Drupal::entityTypeManager()
->getStorage('media_type')
->load($media->bundle());
$source_field = $media->getSource()
->getSourceFieldDefinition($media_type)
->getName();

if (empty($variables['content'][$source_field][0]['#description'])) {
$variables['content'][$source_field][0]['#description'] = $variables['name'];
Expand All @@ -126,3 +133,68 @@ function stanford_media_preprocess_filter_caption(&$variables) {
}
$variables['classes'] = trim($variables['classes'] . ' caption');
}

/**
* Implements hook_ENTITY_TYPE_prepare_form().
*/
function stanford_media_media_prepare_form(MediaInterface $media, $operation, FormStateInterface $form_state) {
/** @var \Drupal\entity_usage\EntityUsageInterface $entity_usage_service */
$entity_usage_service = \Drupal::service('entity_usage.usage');
$sources = $entity_usage_service->listSources($media);
$count = 0;
foreach ($sources as $source) {
$count += count($source);
}
// Display a message to the user to alert them than editing will affect
// multiple pieces of content.
if ($count) {
$message = \Drupal::translation()->formatPlural(
$count,
'Changing this media item will affect %count piece of content.',
'Changing this media item will affect %count pieces of content.',
['%count' => $count]
);
\Drupal::messenger()->addWarning($message);
}

}

/**
* Implements hook_entity_access().
*
* Restrict access to media entities that are used as field default values.
*/
function stanford_media_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {

// Only lock down the media entities since they are the default field values
// that we care about.
if (
$entity->getEntityTypeId() != 'media' ||
!in_array($operation, ['update', 'delete'])
) {
return AccessResult::neutral();
}

$configs = \Drupal::configFactory()->listAll('field.field.');
foreach ($configs as $config_name) {
$config = \Drupal::config($config_name);

// Check for the fields we are interested by checking their type and handler
// settings.
if (
$config->get('field_type') == 'entity_reference' &&
$config->get('settings.handler') == 'default:media' &&
!empty($config->get('default_value'))
) {
$default_value = $config->get('default_value');

// The field default value matches the current media entity so we want to
// forbid editing/deleting if the user doesn't have the proper permission.
if (!empty($default_value[0]['target_uuid']) && $entity->uuid() == $default_value[0]['target_uuid']) {
return AccessResult::forbiddenIf(!$account->hasPermission('edit field default media'), 'The entity is set as a default field value.');
}
}
}

return AccessResult::neutral();
}
3 changes: 3 additions & 0 deletions stanford_media.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
edit field default media:
title: 'Edit Field Default Media'
description: 'Users can edit and replace the images/files/etc set for default field values'

0 comments on commit 9922eeb

Please sign in to comment.