Skip to content

Commit

Permalink
8.3.4
Browse files Browse the repository at this point in the history
- HSD8-1340 Clean up files when a media item is deleted (#132)
- HSD8-1338 Add twitter embed code validator (#131)
  • Loading branch information
pookmish authored Jul 28, 2022
2 parents ad9debf + 059c9fc commit 452c675
Show file tree
Hide file tree
Showing 25 changed files with 315 additions and 19 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.3.4
--------------------------------------------------------------------------------
_Release Date: 2022-07-28_

- HSD8-1340 Clean up files when a media item is deleted (#132)
- HSD8-1338 Add twitter embed code validator (#131)

8.3.3
--------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
],
"require": {
"php": ">=8.0",
"davidbarratt/custom-installer": "~1.0",
"drupal/core": "^9.3",
"drupal/dropzonejs": "^2.0@alpha",
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-3.3
version: 8.x-3.4-dev
package: media
dependencies:
- drupal:media
51 changes: 51 additions & 0 deletions src/Plugin/EmbedValidator/TwitterValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\stanford_media\Plugin\EmbedValidator;

use Drupal\stanford_media\Plugin\EmbedValidatorBase;

/**
* Twitter validation.
*
* @EmbedValidator (
* id = "twitter",
* label = "Twitter"
* )
*/
class TwitterValidator extends EmbedValidatorBase {

/**
* {@inheritDoc}
*/
public function isEmbedCodeAllowed(string $code): bool {
$code = self::prepareEmbedCode($code);
return !empty($code);
}

/**
* {@inheritDoc}
*/
public function prepareEmbedCode(string $code): string {
// Strip out a bunch of stuff we don't care about.
$code = preg_replace("/\r\n|\r|\n/", '', strip_tags($code, '<blockquote> <script> <a> <p>'));

// The twitter widget script keys off of the class name with twitter- for
// the prefix. Like twitter-tweet, twitter-timline, etc. We need to grab
// the element that has that class and then we will retain everything within
// that element.
preg_match('/<(\w+) .*?class="twitter-(.*?)".*?>/', $code, $twitter_container);

// There should only be 1 <script> tag with the twitter domain.
preg_match('/<script.*?src="(.*platform\.twitter\.com.*?)".*?>/', $code, $script_matches);
if (!isset($twitter_container[1]) || !isset($script_matches[0])) {
return '';
}

// Grab the twitter container that we found earlier.
preg_match('/<' . $twitter_container[1] . '.*?\/' . $twitter_container[1] . '>/', $code, $container);

// Combine the container with the script element.
return reset($container) . reset($script_matches) . '</script>';
}

}
102 changes: 101 additions & 1 deletion src/StanfordMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,46 @@

namespace Drupal\stanford_media;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\file\FileUsage\FileUsageInterface;
use Drupal\media\MediaInterface;

/**
* Class StanfordMedia.
*/
class StanfordMedia implements TrustedCallbackInterface {
class StanfordMedia implements StanfordMediaInterface, TrustedCallbackInterface {

use MessengerTrait;
use StringTranslationTrait;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The file usage.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -84,4 +114,74 @@ public static function imageAltValue(array &$element, $input, FormStateInterface
return $input;
}

/**
* Constructs a new StanfordMedia.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
* The file usage.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, FileUsageInterface $file_usage, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->fileUsage = $file_usage;
$this->configFactory = $config_factory;
}

/**
* {@inheritDoc}
*/
public function deleteMediaFiles(MediaInterface $media): void {
// Delete the file from the source field for files and image media types.
if (in_array($media->getSource()->getPluginId(), ['file', 'image'])) {
$media_type = $this->entityTypeManager->getStorage('media_type')
->load($media->bundle());
$source_field = $media->getSource()
->getSourceFieldDefinition($media_type)
->getName();
$this->deleteFileFromField($media, $source_field);
}
// Delete the thumbnail for videos and other media types.
$this->deleteFileFromField($media, 'thumbnail');
}

/**
* Grab the file entity from the media field and delete it.
*
* @param \Drupal\media\MediaInterface $media
* Media entity.
* @param string $field_name
* Field name for the file reference.
*/
protected function deleteFileFromField(MediaInterface $media, string $field_name): void {
if (!$media->hasField($field_name)) {
return;
}
/** @var \Drupal\Core\Field\FieldItemInterface $field_value */
$field_value = $media->get($field_name)->get(0);

/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager->getStorage('file')
->load($field_value->get('target_id')->getString());

$media_icon_path = $this->configFactory->getEditable('media.settings')
->get('icon_base_uri');

// Remove the scheme of the icon path so that we don't delete private or
// public directory icons.
$media_icon_path = strpbrk($media_icon_path, '/');

if (
$file &&
!$this->fileUsage->listUsage($file) &&
!str_contains($file->getFileUri(), $media_icon_path)
) {
$this->messenger()
->addStatus($this->t('Permanently deleted file from the server: @file', ['@file' => $file->getFilename()]));
$file->delete();
}
}

}
20 changes: 20 additions & 0 deletions src/StanfordMediaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\stanford_media;

use Drupal\media\MediaInterface;

/**
* Interface StanfordMediaInterface.
*/
interface StanfordMediaInterface {

/**
* Delete any files associated with this media.
*
* @param \Drupal\media\MediaInterface $media
* Media entity.
*/
public function deleteMediaFiles(MediaInterface $media):void;

}
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-3.3
version: 8.x-3.4
dependencies:
- dropzonejs:dropzonejs
- drupal:breakpoint
Expand Down
7 changes: 7 additions & 0 deletions stanford_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use Drupal\stanford_media\Form\MediaLibraryEmbeddableForm;
use Drupal\stanford_media\Form\MediaLibraryFileUploadForm;
use Drupal\stanford_media\Form\MediaLibraryGoogleFormForm;

/**
* Implements hook_ENTITY_TYPE_delete().
*/
function stanford_media_media_delete(MediaInterface $media) {
\Drupal::service('stanford_media')->deleteMediaFiles($media);
}

/**
* Implements hook_entity_bundle_field_info_alter().
*/
Expand Down
3 changes: 3 additions & 0 deletions stanford_media.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
services:
stanford_media:
class: Drupal\stanford_media\StanfordMedia
arguments: ['@entity_type.manager', '@file.usage', '@config.factory']
stanford_media.route_subscriber:
class: Drupal\stanford_media\Routing\RouteSubscriber
tags:
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/Form/BulkUploadFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\stanford_media\Form\BulkUpload;
use Drupal\Tests\stanford_media\Kernel\StanfordMediaTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;

Expand All @@ -15,7 +16,7 @@
* @group stanford_media
* @coversDefaultClass \Drupal\stanford_media\Form\BulkUpload
*/
class BulkUploadFormTest extends StanfordMediaFormTestBase {
class BulkUploadFormTest extends StanfordMediaTestBase {

/**
* Testing form namespace argument.
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/Form/MediaLibraryEmbeddableFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\stanford_media\Form\MediaLibraryEmbeddableForm;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\stanford_media\Kernel\StanfordMediaTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
Expand All @@ -17,7 +18,7 @@
* @group stanford_media
* @coversDefaultClass \Drupal\stanford_media\Form\MediaLibraryEmbeddableForm
*/
class MediaLibraryEmbeddableFormTest extends StanfordMediaFormTestBase {
class MediaLibraryEmbeddableFormTest extends StanfordMediaTestBase {

use UserCreationTrait;

Expand Down
9 changes: 5 additions & 4 deletions tests/src/Kernel/Form/MediaLibraryFileUploadFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use Drupal\media_duplicate_validation\Plugin\MediaDuplicateValidationManager;
use Drupal\media_library\MediaLibraryState;
use Drupal\stanford_media\Form\MediaLibraryFileUploadForm;
use Drupal\Tests\stanford_media\Kernel\StanfordMediaTestBase;

/**
* Class MediaLibraryFileUploadFormTest.
*
* @group stanford_media
* @coversDefaultClass \Drupal\stanford_media\Form\MediaLibraryFileUploadForm
*/
class MediaLibraryFileUploadFormTest extends StanfordMediaFormTestBase {
class MediaLibraryFileUploadFormTest extends StanfordMediaTestBase {

/**
* Testing form namespace argument.
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testFormStructure() {
* @throws \Drupal\Core\Form\FormAjaxException
*/
public function testEntityFormElement() {
list($form, $form_state) = $this->runEntityFormSetup();
[$form, $form_state] = $this->runEntityFormSetup();
$this->assertArrayNotHasKey('similar_media', $form['media'][0]);
}

Expand All @@ -120,7 +121,7 @@ public function testEntityFormElement() {
*/
public function testEntityFormWithSimilar() {
$this->addDuplicationService();
list($form, $form_state) = $this->runEntityFormSetup();
[$form, $form_state] = $this->runEntityFormSetup();
$this->assertCount(4, $form['media'][0]['similar_media'][0]['#options']);
}

Expand Down Expand Up @@ -165,7 +166,7 @@ protected function runEntityFormSetup() {
public function testValidation() {
$this->addDuplicationService();
/** @var \Drupal\Core\Form\FormStateInterface $form_state */
list($form, $form_state) = $this->runEntityFormSetup();
[$form, $form_state] = $this->runEntityFormSetup();
$form_state->setValue(['similar_media'], [2]);
$form_state->getFormObject()->validateForm($form, $form_state);
$this->assertEquals(2, $form_state->get(['media', 0])->id());
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/Form/MediaLibraryGoogleFormFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\media\Entity\MediaType;
use Drupal\media_library\MediaLibraryState;
use Drupal\stanford_media\Form\MediaLibraryGoogleFormForm;
use Drupal\Tests\stanford_media\Kernel\StanfordMediaTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
Expand All @@ -15,7 +16,7 @@
* @group stanford_media
* @coversDefaultClass \Drupal\stanford_media\Form\MediaLibraryGoogleFormForm
*/
class MediaLibraryGoogleFormFormTest extends StanfordMediaFormTestBase {
class MediaLibraryGoogleFormFormTest extends StanfordMediaTestBase {

use UserCreationTrait;

Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/Form/StanfordMediaDialogFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\media\Entity\Media;
use Drupal\Tests\stanford_media\Kernel\StanfordMediaTestBase;

/**
* Class StanfordMediaDialogFormTest.
*
* @group stanford_media
* @coversDefaultClass \Drupal\stanford_media\Form\StanfordMediaDialogForm
*/
class StanfordMediaDialogFormTest extends StanfordMediaFormTestBase {
class StanfordMediaDialogFormTest extends StanfordMediaTestBase {

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected function setUp(): void {
$this->installEntitySchema('file');
$this->installSchema('file', ['file_usage']);
$this->installEntitySchema('media');
$this->installConfig('media');

// Ceate a Date Format.
DateFormat::create([
Expand Down
Loading

0 comments on commit 452c675

Please sign in to comment.