From 8252ca8cbd07298a4ce5b5d69194eee308800d90 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Thu, 15 Jul 2021 16:29:46 +0200 Subject: [PATCH 1/9] feat: teaser preview --- ...tity_form_display.node.article.default.yml | 37 +++++++------ modules/thunder_article/js/teaser_preview.js | 55 +++++++++++++++++++ .../src/Form/ThunderNodeForm.php | 17 ++++++ .../thunder_article.libraries.yml | 6 ++ .../thunder_article/thunder_article.module | 12 ++++ 5 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 modules/thunder_article/js/teaser_preview.js create mode 100644 modules/thunder_article/thunder_article.libraries.yml diff --git a/config/optional/core.entity_form_display.node.article.default.yml b/config/optional/core.entity_form_display.node.article.default.yml index 06e786ca8..2d721c923 100644 --- a/config/optional/core.entity_form_display.node.article.default.yml +++ b/config/optional/core.entity_form_display.node.article.default.yml @@ -29,12 +29,12 @@ third_party_settings: children: - field_teaser_media - field_teaser_text + - article_teaser_preview_wrapper parent_name: '' - weight: 4 + weight: 5 label: Teaser format_type: fieldset format_settings: - label: Teaser description: '' required_fields: true id: '' @@ -44,11 +44,10 @@ third_party_settings: children: - field_paragraphs parent_name: '' - weight: 6 + weight: 7 label: Paragraphs format_type: fieldset format_settings: - label: Paragraphs description: '' required_fields: true id: '' @@ -64,7 +63,6 @@ third_party_settings: weight: 3 format_type: fieldset format_settings: - label: Basis description: '' required_fields: true id: '' @@ -76,6 +74,11 @@ targetEntityType: node bundle: article mode: default content: + article_teaser_preview_wrapper: + weight: 6 + region: content + settings: { } + third_party_settings: { } created: type: datetime_timestamp weight: 1 @@ -109,9 +112,9 @@ content: form_display_mode: default default_paragraph_type: _none features: - duplicate: '0' - collapse_edit_all: '0' add_above: '0' + collapse_edit_all: '0' + duplicate: '0' third_party_settings: paragraphs_features: delete_confirmation: true @@ -143,6 +146,7 @@ content: autocomplete: true match_operator: CONTAINS width: 100% + match_limit: 10 third_party_settings: { } region: content field_teaser_media: @@ -161,7 +165,7 @@ content: type: entity_browser_entity_reference region: content field_teaser_text: - weight: 6 + weight: 5 settings: rows: 5 placeholder: '' @@ -177,13 +181,13 @@ content: region: content moderation_state: type: thunder_moderation_state_default - weight: 100 + weight: 17 settings: { } region: content third_party_settings: { } path: type: path - weight: 8 + weight: 9 settings: { } third_party_settings: { } region: content @@ -191,7 +195,7 @@ content: type: boolean_checkbox settings: display_label: true - weight: 5 + weight: 6 third_party_settings: { } region: content publish_on: @@ -202,7 +206,7 @@ content: region: content publish_state: type: scheduler_moderation - weight: 30 + weight: 14 region: content settings: { } third_party_settings: { } @@ -215,14 +219,14 @@ content: type: boolean_checkbox settings: display_label: true - weight: 120 + weight: 18 region: content third_party_settings: { } sticky: type: boolean_checkbox settings: display_label: true - weight: 7 + weight: 8 third_party_settings: { } region: content title: @@ -240,6 +244,7 @@ content: width: 100% autocomplete: true match_operator: CONTAINS + match_limit: 10 third_party_settings: { } region: content unpublish_on: @@ -250,12 +255,12 @@ content: region: content unpublish_state: type: scheduler_moderation - weight: 30 + weight: 15 region: content settings: { } third_party_settings: { } url_redirects: - weight: 50 + weight: 16 region: content settings: { } third_party_settings: { } diff --git a/modules/thunder_article/js/teaser_preview.js b/modules/thunder_article/js/teaser_preview.js new file mode 100644 index 000000000..3f2b5c8a1 --- /dev/null +++ b/modules/thunder_article/js/teaser_preview.js @@ -0,0 +1,55 @@ +(function (Drupal) { + + 'use strict'; + + /** + * Define teaser preview template. + * + * @param {object} config + * Configuration options for teaser preview template. + * + * @return {string} + * Returns markup string for teaser preview. + */ + Drupal.theme.thunderArticleTeaserPreview = function (config) { + return '' + + '' + + '

' + config.title + '

' + + '

' + config.text + '

'; + }; + + + Drupal.behaviors.teaserPreview = { + attach: function (context, settings) { + + var title = document.querySelector('[data-drupal-selector="edit-field-seo-title-0-value"]') + var text = document.querySelector('[data-drupal-selector="edit-field-teaser-text-0-value"]') + console.log(text) + + title.addEventListener('input', (e) => { + var titleField = document.querySelector('article.teaser-preview h1') + titleField.textContent = e.target.value + }) + + text.addEventListener('input', (e) => { + var textField = document.querySelector('article.teaser-preview p') + textField.innerHTML = e.target.value.replace(/(?:\r\n|\r|\n)/g, '
'); + }) + + var imageSrc = '' + var imageField = document.querySelector('[data-drupal-selector="edit-field-teaser-media-wrapper"] img') + if (imageField) { + imageSrc = imageField.src + } + + var container = document.querySelector('article.teaser-preview') + container.innerHTML = Drupal.theme('thunderArticleTeaserPreview', { + image: imageSrc, + title: title.value, + text: text.value.replace(/(?:\r\n|\r|\n)/g, '
') + }); + + } + }; + +})(Drupal); diff --git a/modules/thunder_article/src/Form/ThunderNodeForm.php b/modules/thunder_article/src/Form/ThunderNodeForm.php index 185d7a822..77d77f644 100644 --- a/modules/thunder_article/src/Form/ThunderNodeForm.php +++ b/modules/thunder_article/src/Form/ThunderNodeForm.php @@ -124,6 +124,23 @@ public function formAlter(array &$form, FormStateInterface $form_state) { $form['actions'] = array_merge($form['actions'], $this->actions($entity)); + if ($field = $form_state->get('form_display')->getComponent('article_teaser_preview_wrapper')) { + $form['article_teaser_preview_wrapper'] = [ + '#type' => 'container', + ]; + $form['article_teaser_preview_wrapper']['article_teaser_preview'] = [ + '#type' => 'html_tag', + '#tag' => 'article', + '#weight' => $field['weight'], + '#attributes' => [ + 'class' => ['teaser-preview'], + ], + '#attached' => [ + 'library' => ['thunder_article/teaser_preview'], + ], + ]; + } + return $form; } diff --git a/modules/thunder_article/thunder_article.libraries.yml b/modules/thunder_article/thunder_article.libraries.yml new file mode 100644 index 000000000..f49397074 --- /dev/null +++ b/modules/thunder_article/thunder_article.libraries.yml @@ -0,0 +1,6 @@ +teaser_preview: + version: VERSION + js: + js/teaser_preview.js: {} + dependencies: + - core/drupal diff --git a/modules/thunder_article/thunder_article.module b/modules/thunder_article/thunder_article.module index 19097b839..3a93f97e5 100644 --- a/modules/thunder_article/thunder_article.module +++ b/modules/thunder_article/thunder_article.module @@ -38,3 +38,15 @@ function thunder_article_form_node_form_alter(&$form, FormStateInterface $form_s ->getInstanceFromDefinition(ThunderNodeForm::class) ->formAlter($form, $form_state); } + +/** + * Implements hook_entity_extra_field_info(). + */ +function thunder_article_entity_extra_field_info() { + $extra['node']['article']['form']['article_teaser_preview_wrapper'] = [ + 'label' => t('Teaser Preview'), + 'weight' => 100, + 'visible' => TRUE, + ]; + return $extra; +} From 348e524d4ad9ae7364362763023a41c245b64b23 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Thu, 15 Jul 2021 17:00:26 +0200 Subject: [PATCH 2/9] fix: config --- .../core.entity_form_display.node.article.bulk_edit.yml | 1 + .../core.entity_form_display.node.article.default.yml | 7 ++++++- modules/thunder_article/src/Form/ThunderNodeForm.php | 9 +++++---- modules/thunder_article/thunder_article.module | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/config/optional/core.entity_form_display.node.article.bulk_edit.yml b/config/optional/core.entity_form_display.node.article.bulk_edit.yml index 24b4c7459..81f97f9b1 100644 --- a/config/optional/core.entity_form_display.node.article.bulk_edit.yml +++ b/config/optional/core.entity_form_display.node.article.bulk_edit.yml @@ -58,6 +58,7 @@ content: match_limit: 10 third_party_settings: { } hidden: + article_teaser_preview: true created: true field_meta_tags: true field_paragraphs: true diff --git a/config/optional/core.entity_form_display.node.article.default.yml b/config/optional/core.entity_form_display.node.article.default.yml index 2d721c923..1f2dabc1b 100644 --- a/config/optional/core.entity_form_display.node.article.default.yml +++ b/config/optional/core.entity_form_display.node.article.default.yml @@ -29,7 +29,7 @@ third_party_settings: children: - field_teaser_media - field_teaser_text - - article_teaser_preview_wrapper + - article_teaser_preview parent_name: '' weight: 5 label: Teaser @@ -74,6 +74,11 @@ targetEntityType: node bundle: article mode: default content: + article_teaser_preview: + weight: 6 + region: content + settings: { } + third_party_settings: { } article_teaser_preview_wrapper: weight: 6 region: content diff --git a/modules/thunder_article/src/Form/ThunderNodeForm.php b/modules/thunder_article/src/Form/ThunderNodeForm.php index 77d77f644..593702e99 100644 --- a/modules/thunder_article/src/Form/ThunderNodeForm.php +++ b/modules/thunder_article/src/Form/ThunderNodeForm.php @@ -124,14 +124,15 @@ public function formAlter(array &$form, FormStateInterface $form_state) { $form['actions'] = array_merge($form['actions'], $this->actions($entity)); - if ($field = $form_state->get('form_display')->getComponent('article_teaser_preview_wrapper')) { - $form['article_teaser_preview_wrapper'] = [ + if ($field = $form_state->get('form_display')->getComponent('article_teaser_preview')) { + $form['article_teaser_preview'] = [ '#type' => 'container', + '#weight' => $field['weight'], + ]; - $form['article_teaser_preview_wrapper']['article_teaser_preview'] = [ + $form['article_teaser_preview']['article'] = [ '#type' => 'html_tag', '#tag' => 'article', - '#weight' => $field['weight'], '#attributes' => [ 'class' => ['teaser-preview'], ], diff --git a/modules/thunder_article/thunder_article.module b/modules/thunder_article/thunder_article.module index 3a93f97e5..d168d5010 100644 --- a/modules/thunder_article/thunder_article.module +++ b/modules/thunder_article/thunder_article.module @@ -43,7 +43,7 @@ function thunder_article_form_node_form_alter(&$form, FormStateInterface $form_s * Implements hook_entity_extra_field_info(). */ function thunder_article_entity_extra_field_info() { - $extra['node']['article']['form']['article_teaser_preview_wrapper'] = [ + $extra['node']['article']['form']['article_teaser_preview'] = [ 'label' => t('Teaser Preview'), 'weight' => 100, 'visible' => TRUE, From e0c3a4eee240be45a5b503f18ea29ab4bfa3e94b Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Thu, 15 Jul 2021 17:08:44 +0200 Subject: [PATCH 3/9] fix: coding style --- modules/thunder_article/js/teaser_preview.js | 55 ++++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/modules/thunder_article/js/teaser_preview.js b/modules/thunder_article/js/teaser_preview.js index 3f2b5c8a1..414d79040 100644 --- a/modules/thunder_article/js/teaser_preview.js +++ b/modules/thunder_article/js/teaser_preview.js @@ -1,7 +1,4 @@ -(function (Drupal) { - - 'use strict'; - +((Drupal) => { /** * Define teaser preview template. * @@ -11,45 +8,47 @@ * @return {string} * Returns markup string for teaser preview. */ - Drupal.theme.thunderArticleTeaserPreview = function (config) { - return '' + - '' + - '

' + config.title + '

' + - '

' + config.text + '

'; + Drupal.theme.thunderArticleTeaserPreview = (config) => { + return ( + `` + + `

${config.title}

` + + `

${config.text}

` + ); }; - Drupal.behaviors.teaserPreview = { - attach: function (context, settings) { - - var title = document.querySelector('[data-drupal-selector="edit-field-seo-title-0-value"]') - var text = document.querySelector('[data-drupal-selector="edit-field-teaser-text-0-value"]') - console.log(text) + attach() { + const title = document.querySelector( + '[data-drupal-selector="edit-field-seo-title-0-value"]', + ); + const text = document.querySelector( + '[data-drupal-selector="edit-field-teaser-text-0-value"]', + ); title.addEventListener('input', (e) => { - var titleField = document.querySelector('article.teaser-preview h1') - titleField.textContent = e.target.value - }) + const titleField = document.querySelector('article.teaser-preview h1'); + titleField.textContent = e.target.value; + }); text.addEventListener('input', (e) => { - var textField = document.querySelector('article.teaser-preview p') + const textField = document.querySelector('article.teaser-preview p'); textField.innerHTML = e.target.value.replace(/(?:\r\n|\r|\n)/g, '
'); - }) + }); - var imageSrc = '' - var imageField = document.querySelector('[data-drupal-selector="edit-field-teaser-media-wrapper"] img') + let imageSrc = ''; + const imageField = document.querySelector( + '[data-drupal-selector="edit-field-teaser-media-wrapper"] img', + ); if (imageField) { - imageSrc = imageField.src + imageSrc = imageField.src; } - var container = document.querySelector('article.teaser-preview') + const container = document.querySelector('article.teaser-preview'); container.innerHTML = Drupal.theme('thunderArticleTeaserPreview', { image: imageSrc, title: title.value, - text: text.value.replace(/(?:\r\n|\r|\n)/g, '
') + text: text.value.replace(/(?:\r\n|\r|\n)/g, '
'), }); - - } + }, }; - })(Drupal); From 1c35b1adfef89bf5dcd70ab418c342d995bc332a Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Fri, 16 Jul 2021 09:03:37 +0200 Subject: [PATCH 4/9] fix: remove old key --- .../core.entity_form_display.node.article.default.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config/optional/core.entity_form_display.node.article.default.yml b/config/optional/core.entity_form_display.node.article.default.yml index 1f2dabc1b..4f9339710 100644 --- a/config/optional/core.entity_form_display.node.article.default.yml +++ b/config/optional/core.entity_form_display.node.article.default.yml @@ -79,11 +79,6 @@ content: region: content settings: { } third_party_settings: { } - article_teaser_preview_wrapper: - weight: 6 - region: content - settings: { } - third_party_settings: { } created: type: datetime_timestamp weight: 1 From 643d91adb9af34a19c42feda4f701c8f67f88225 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Mon, 19 Jul 2021 09:29:42 +0200 Subject: [PATCH 5/9] fix: trim text --- modules/thunder_article/js/teaser_preview.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/thunder_article/js/teaser_preview.js b/modules/thunder_article/js/teaser_preview.js index 414d79040..1e141e0f8 100644 --- a/modules/thunder_article/js/teaser_preview.js +++ b/modules/thunder_article/js/teaser_preview.js @@ -27,12 +27,15 @@ title.addEventListener('input', (e) => { const titleField = document.querySelector('article.teaser-preview h1'); - titleField.textContent = e.target.value; + titleField.textContent = this.trimText(e.target.value, 100); }); text.addEventListener('input', (e) => { const textField = document.querySelector('article.teaser-preview p'); - textField.innerHTML = e.target.value.replace(/(?:\r\n|\r|\n)/g, '
'); + textField.innerHTML = this.trimText(e.target.value, 155).replace( + /(?:\r\n|\r|\n)/g, + '
', + ); }); let imageSrc = ''; @@ -46,9 +49,15 @@ const container = document.querySelector('article.teaser-preview'); container.innerHTML = Drupal.theme('thunderArticleTeaserPreview', { image: imageSrc, - title: title.value, - text: text.value.replace(/(?:\r\n|\r|\n)/g, '
'), + title: this.trimText(title.value, 100), + text: this.trimText(text.value, 155).replace(/(?:\r\n|\r|\n)/g, '
'), }); }, + + trimText: (string, length) => { + return string.length > length + ? `${string.substring(0, length - 3)}...` + : string; + }, }; })(Drupal); From 62caf16d0c6c6debcf0debaf4516f067b5d055c1 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Tue, 20 Jul 2021 16:57:42 +0200 Subject: [PATCH 6/9] fix: use preview theme branch --- .github/workflows/tests.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7ac1a26f6..ed35d0ceb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,7 +18,7 @@ env: DRUPAL_TESTING_TEST_PROFILE: thunder DRUPAL_TESTING_TEST_GROUP: Thunder DRUPAL_TESTING_HTTP_PORT: 8888 - THUNDER_ADMIN_BRANCH: 4.x + THUNDER_ADMIN_BRANCH: feat/teaser_preview PHPSTAN_MEMORY_LIMIT: 4G jobs: diff --git a/composer.json b/composer.json index 7d9a0c636..aeffb8b2a 100644 --- a/composer.json +++ b/composer.json @@ -110,7 +110,7 @@ "drupal/simple_sitemap": "^3.10", "drupal/shariff": "^1.5", "drupal/slick": "^2.2", - "drupal/thunder_admin": "^4.0", + "drupal/thunder_admin": "dev-feat/teaser_preview", "drupal/token": "^1.7", "drupal/update_helper": "^2.0", "drupal/search_api": "^1.17", From a5a0557294bda11e11f5d51280ecc077f50953e0 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Tue, 20 Jul 2021 17:07:44 +0200 Subject: [PATCH 7/9] fix: theme verion --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index aeffb8b2a..7d9a0c636 100644 --- a/composer.json +++ b/composer.json @@ -110,7 +110,7 @@ "drupal/simple_sitemap": "^3.10", "drupal/shariff": "^1.5", "drupal/slick": "^2.2", - "drupal/thunder_admin": "dev-feat/teaser_preview", + "drupal/thunder_admin": "^4.0", "drupal/token": "^1.7", "drupal/update_helper": "^2.0", "drupal/search_api": "^1.17", From 43ccbedc24ed7b0e9a0221e468a1515ac6c7a078 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Wed, 21 Jul 2021 10:57:19 +0200 Subject: [PATCH 8/9] fix: add dependencies --- modules/thunder_article/thunder_article.module | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/thunder_article/thunder_article.module b/modules/thunder_article/thunder_article.module index d970ed5a0..94b460bc7 100644 --- a/modules/thunder_article/thunder_article.module +++ b/modules/thunder_article/thunder_article.module @@ -21,6 +21,17 @@ function thunder_article_form_node_form_alter(&$form, FormStateInterface $form_s * Implements hook_entity_extra_field_info(). */ function thunder_article_entity_extra_field_info() { + /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ + $field_manager = \Drupal::service('entity_field.manager'); + $fields = $field_manager->getFieldDefinitions('node', 'article'); + $required_fields = [ + 'field_seo_title', + 'field_teaser_text', + 'field_teaser_media', + ]; + if (!empty(array_diff($required_fields, array_keys($fields)))) { + return []; + } $extra['node']['article']['form']['article_teaser_preview'] = [ 'label' => t('Teaser Preview'), 'weight' => 100, From 0a78c2e92115f742c1e0fa3c389eb4a6bcbf39d8 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Wed, 21 Jul 2021 11:11:04 +0200 Subject: [PATCH 9/9] fix: title position --- .../core.entity_form_display.node.article.default.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/optional/core.entity_form_display.node.article.default.yml b/config/optional/core.entity_form_display.node.article.default.yml index 4f9339710..3bfc11fe7 100644 --- a/config/optional/core.entity_form_display.node.article.default.yml +++ b/config/optional/core.entity_form_display.node.article.default.yml @@ -27,6 +27,7 @@ third_party_settings: field_group: group_teaser: children: + - field_seo_title - field_teaser_media - field_teaser_text - article_teaser_preview @@ -57,7 +58,6 @@ third_party_settings: children: - field_channel - title - - field_seo_title - field_tags parent_name: '' weight: 3 @@ -75,7 +75,7 @@ bundle: article mode: default content: article_teaser_preview: - weight: 6 + weight: 7 region: content settings: { } third_party_settings: { } @@ -150,7 +150,7 @@ content: third_party_settings: { } region: content field_teaser_media: - weight: 4 + weight: 5 settings: entity_browser: image_browser field_widget_display: rendered_entity @@ -165,7 +165,7 @@ content: type: entity_browser_entity_reference region: content field_teaser_text: - weight: 5 + weight: 6 settings: rows: 5 placeholder: ''