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

Settings for media alias display for documents type #557

Open
wants to merge 6 commits into
base: develop
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"codemirror/codemirror": "5.65.12",
"jquery/inputmask": "5.0.8",
"jquery/intl-tel-input": "17.0.19",
"progress-tracker/progress-tracker": "2.0.7"
"progress-tracker/progress-tracker": "2.0.7",
"drupal/media_alias_display": "^2.1"
},
"repositories": {
"drupal": {
Expand Down
15 changes: 15 additions & 0 deletions modules/tide_media/tide_media.install
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,18 @@ function tide_media_update_10005() {
$config->set('track_enabled_target_entity_types', $result);
$config->save();
}

/**
* Adds media_alias_display settings for document type.
*/
function tide_media_update_10006() {
if (\Drupal::moduleHandler()->moduleExists('media_alias_display') === FALSE) {
\Drupal::service('module_installer')->install(['media_alias_display']);
}

$media_config = \Drupal::service('config.factory')->getEditable('media_alias_display.settings');
if ($media_config) {
$media_config->set('media_bundles', ['document' => 'document']);
$media_config->save();
}
}
75 changes: 75 additions & 0 deletions tide_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use Drupal\Core\Access\AccessResult;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\redirect\Entity\Redirect;
use Drupal\scheduled_transitions\Routing\ScheduledTransitionsRouteProvider;
use Drupal\tide_core\Render\Element\AdminToolbar;
Expand Down Expand Up @@ -694,6 +697,7 @@ function tide_core_node_presave(NodeInterface $node) {
$node_state = isset($node->get('moderation_state')->getValue()[0]) ? $node->get('moderation_state')->getValue()[0]['value'] : '';

if ($node_state === 'published') {
tide_core_media_autopublish($node);
$log = [
/*
* Term the 'type' => 'node' as 'type' => 'page'
Expand Down Expand Up @@ -786,3 +790,74 @@ function tide_core_user_role_insert(RoleInterface $role): void {
$role->save();
}
}

/**
* Publish unpublished media(documents) on node publish.
*/
function tide_core_media_autopublish($node) {
// Loop through all fields of the node.
foreach ($node->getFields() as $field_name => $field) {

// Check if the field is of type Entity Reference to Paragraphs.
if ($field->getFieldDefinition()->getType() == 'entity_reference_revisions' &&
$field->getFieldDefinition()->getSetting('target_type') == 'paragraph') {

// Loop through all referenced Paragraph entities.
foreach ($field->getValue() as $paragraph_item) {
$paragraph = Paragraph::load($paragraph_item['target_id']);
if ($paragraph) {
// Loop through fields in the paragraph.
foreach ($paragraph->getFields() as $sub_field_name => $sub_field) {
// Check if the field is of type "Text (formatted, long)".
if ($sub_field instanceof FieldItemListInterface &&
$sub_field->getFieldDefinition()->getType() == 'text_long') {
$text_content = $sub_field->value;
preg_match_all('/data-entity-uuid="([a-f0-9\-]+)"/', $text_content, $matches);
if (!empty($matches[1])) {
tide_core_process_media_entity_status($matches[1]);
}
}
}
}
}
}

// Check if the field is the "Body" field (common for content types)
// Ensure the body field is of type "Text (formatted, long)".
elseif ($field_name == 'body' && $field->getFieldDefinition()->getType() == 'text_long') {
$body_content = $field->value;
// Parse the body content for media entities using a regex pattern.
preg_match_all('/data-entity-uuid="([a-f0-9\-]+)"/', $body_content, $matches);

// Loop through the found media UUIDs.
if (!empty($matches[1])) {
tide_core_process_media_entity_status($matches[1]);
}
}
}
}

/**
* Helper function to check and update the media entity status.
*
* @param array $media_uuids
* Array of media UUIDs to process.
*/
function tide_core_process_media_entity_status(array $media_uuids) {
foreach ($media_uuids as $media_uuid) {
// Load the media entity by UUID.
$media = \Drupal::entityTypeManager()->getStorage('media')->loadByProperties(['uuid' => $media_uuid]);
if ($media) {
$media = reset($media);
// Check if the media is an instance of Media.
if ($media instanceof Media) {
$status = $media->get('status')->value;
// If unpublished, set the status to published (1)
if ($status == 0) {
$media->set('status', 1);
$media->save();
}
}
}
}
}