From ba75329d96fba93b730b1a6165112a96aa0a39a9 Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Wed, 27 Nov 2024 14:30:26 +1100 Subject: [PATCH 1/6] Settings for media alias display for documents type --- composer.json | 3 ++- config/install/media_alias_display.settings.yml | 4 ++++ modules/tide_media/tide_media.install | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 config/install/media_alias_display.settings.yml diff --git a/composer.json b/composer.json index 75d430f8a..f2c32c975 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/install/media_alias_display.settings.yml b/config/install/media_alias_display.settings.yml new file mode 100644 index 000000000..d02fa1453 --- /dev/null +++ b/config/install/media_alias_display.settings.yml @@ -0,0 +1,4 @@ +_core: + default_config_hash: WYvSMTu0VCW_FhkuJjHuUhmJ9nerYL0JtZrvGJajNig +kill_switch: false +media_bundles: { } diff --git a/modules/tide_media/tide_media.install b/modules/tide_media/tide_media.install index dc5a250da..58cbc0f60 100644 --- a/modules/tide_media/tide_media.install +++ b/modules/tide_media/tide_media.install @@ -81,3 +81,19 @@ 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(); + } +} + From d84578d427e4ba79f9c4599509d30fcbdb09b9f4 Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Thu, 28 Nov 2024 16:13:51 +1100 Subject: [PATCH 2/6] Removed config --- config/install/media_alias_display.settings.yml | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 config/install/media_alias_display.settings.yml diff --git a/config/install/media_alias_display.settings.yml b/config/install/media_alias_display.settings.yml deleted file mode 100644 index d02fa1453..000000000 --- a/config/install/media_alias_display.settings.yml +++ /dev/null @@ -1,4 +0,0 @@ -_core: - default_config_hash: WYvSMTu0VCW_FhkuJjHuUhmJ9nerYL0JtZrvGJajNig -kill_switch: false -media_bundles: { } From 36e70ca59d46e656994f3efb415432bcf8d8a329 Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Fri, 29 Nov 2024 11:05:47 +1100 Subject: [PATCH 3/6] Fixed line spaces --- modules/tide_media/tide_media.install | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/tide_media/tide_media.install b/modules/tide_media/tide_media.install index 58cbc0f60..811c73658 100644 --- a/modules/tide_media/tide_media.install +++ b/modules/tide_media/tide_media.install @@ -91,9 +91,8 @@ function tide_media_update_10006() { } $media_config = \Drupal::service('config.factory')->getEditable('media_alias_display.settings'); - if ($media_config ) { - $media_config->set('media_bundles', ['document' => 'document']); - $media_config->save(); + if ($media_config) { + $media_config->set('media_bundles', ['document' => 'document']); + $media_config->save(); } } - From 5b6876ecaa062bab4603a2f1a2d5a7c3b019c4fa Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Fri, 13 Dec 2024 11:07:24 +1100 Subject: [PATCH 4/6] Autopublish media on node publish --- tide_core.module | 113 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 19 deletions(-) diff --git a/tide_core.module b/tide_core.module index 7fab15386..6eff4b6c5 100644 --- a/tide_core.module +++ b/tide_core.module @@ -5,25 +5,28 @@ * Contains tide_core.module. */ -use Drupal\Component\Utility\Html; -use Drupal\Core\Access\AccessResult; -use Drupal\Core\Breadcrumb\Breadcrumb; -use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\EntityInterface; -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\node\Entity\Node; -use Drupal\node\NodeInterface; -use Drupal\redirect\Entity\Redirect; -use Drupal\scheduled_transitions\Routing\ScheduledTransitionsRouteProvider; -use Drupal\tide_core\Render\Element\AdminToolbar; -use Drupal\user\RoleInterface; -use Drupal\views\ViewExecutable; -use Drupal\workflows\Entity\Workflow; + use Drupal\Component\Utility\Html; + 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; + use Drupal\user\RoleInterface; + use Drupal\views\ViewExecutable; + use Drupal\workflows\Entity\Workflow; /** * Implements hook_views_data(). @@ -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' @@ -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 (EntityReferenceRevisions) + 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(); + } + } + } + } +} From 2d647263e5da407e8cfc2a29b21a7526731122d2 Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Fri, 13 Dec 2024 11:20:07 +1100 Subject: [PATCH 5/6] Code spacing fix --- tide_core.module | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tide_core.module b/tide_core.module index 6eff4b6c5..bbfab34dd 100644 --- a/tide_core.module +++ b/tide_core.module @@ -5,28 +5,28 @@ * Contains tide_core.module. */ - use Drupal\Component\Utility\Html; - 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; - use Drupal\user\RoleInterface; - use Drupal\views\ViewExecutable; - use Drupal\workflows\Entity\Workflow; +use Drupal\Component\Utility\Html; +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; +use Drupal\user\RoleInterface; +use Drupal\views\ViewExecutable; +use Drupal\workflows\Entity\Workflow; /** * Implements hook_views_data(). From fb439de0d63365a4f290d21653bc1225f6e841e3 Mon Sep 17 00:00:00 2001 From: sharmasahil Date: Fri, 13 Dec 2024 11:40:02 +1100 Subject: [PATCH 6/6] Fix line length --- tide_core.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tide_core.module b/tide_core.module index bbfab34dd..b7f37c36d 100644 --- a/tide_core.module +++ b/tide_core.module @@ -798,7 +798,7 @@ 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 (EntityReferenceRevisions) + // Check if the field is of type Entity Reference to Paragraphs. if ($field->getFieldDefinition()->getType() == 'entity_reference_revisions' && $field->getFieldDefinition()->getSetting('target_type') == 'paragraph') {