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

[SD-22]Updated hero image theme according header style #516

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

yeniatencio
Copy link
Contributor

@yeniatencio yeniatencio commented Oct 1, 2024

Jira

https://digital-vic.atlassian.net/browse/SD-22
BE testing link: https://nginx-php.pr-1711.content-vic.sdp4.sdp.vic.gov.au/

Problem/Motivation

We want to have these scenarios for customised header:

  1. Corner graphics - Light + Dark (Both options available and by default Light should be selected),
  2. Full background image - Dark only (Only one option should be set)
  3. CTA - Light only (Only one option should be set).
  4. Default appearance - Light only (Only one option should be set)

Fix

  1. Set field field_landing_page_hero_theme to mandatory.
  2. Remove a patch for drupal/jsonapi_extras as new released was done and patch is not needed anymore.
  3. Fix typo in label : Here image theme.
  4. Updated behat test.
  5. Updated tide publication module to make it work in publication and publication page content type.

Related PRs

Screenshots

Screenshot 2024-10-03 at 10 01 49 am Screenshot 2024-10-03 at 10 01 36 am Screenshot 2024-10-03 at 10 01 25 am Screenshot 2024-10-03 at 10 01 12 am

TODO

Copy link
Contributor

@MdNadimHossain MdNadimHossain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
},
};
})(Drupal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not want use js if it is can be done without it. Read the SD, I have mentioned about conditional logic. You will find other examples in landing page where we have used conditional logic states. Take example from there and try to work it out using states instead of adding this JS please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MdNadimHossain agreed. I've already using conditional logic for enable/disable the field_landing_page_hero_theme as per the requirements. However, one of the requirements is to set the default value of one field based on the value of another field and the only Drupal States that can be applied to a form field element are:

  • enabled
  • disabled
  • required
  • optional
  • visible
  • invisible
  • checked
  • unchecked
  • expanded
  • collapsed

As you can notice the default value can't be changed using states. Also, all the examples in landing page module is for changing one of the states in the list above but a default value.
Therefore, that's why I ended up writing JS code for it. Hope it makes sense now.

@yeniatencio yeniatencio changed the title [SD-22]added logic for landing page [SD-22]Updated hero image theme according header style Oct 8, 2024
Copy link
Contributor

@MdNadimHossain MdNadimHossain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yeniatencio I have suggested some suggestions. Please have a look into the revised version and it should work in the same way -

(function (Drupal) {
  "use strict";

  Drupal.behaviors.heroImageTheme = {
    attach: function (context, settings) {
      const imageTheme = context.querySelector(
        'select[data-drupal-selector^="edit-field-landing-page-hero-theme"]'
      );

      if (imageTheme !== null) {
        setImageTheme();

        const headerStyles = context.querySelectorAll(
          'input[data-drupal-selector^="edit-header-style-options"]'
        );

        if (headerStyles.length > 0) {
          headerStyles.forEach((style) => {
            style.addEventListener("change", setImageTheme);
          });
        }
      }

      function setImageTheme() {
        const selectedHeaderStyle = document.querySelector(
          'input[data-drupal-selector^="edit-header-style-options"]:checked'
        );

        if (!selectedHeaderStyle) {
          return; // Exit if no style option is selected.
        }

        let defaultHeaderStyle = selectedHeaderStyle.value;
        imageTheme.value = defaultHeaderStyle === "fullwidth" ? "dark" : "light";
      }
    },
  };
})(Drupal);


Drupal.behaviors.heroImageTheme = {
attach: function (context, settings) {
const imageTheme = document.querySelector(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use context to ensure that the behavior only attaches to elements in the current page context.

'input[data-drupal-selector^="edit-header-style-options"]'
);

if (headerStyles !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check if (headerStyles !== null) should instead check for headerStyles.length > 0. It’s better to check if any elements are found before adding listeners.

}

function setImageTheme() {
let defaultHeaderStyle = document.querySelector(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a guard clause to prevent this error in case no radio buttons are checked

if (headerStyles !== null) {
headerStyles.forEach((style) => {
style.addEventListener("change", () => {
setImageTheme();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeatedly defining an anonymous function for style.addEventListener("change", ...), you can directly pass the reference to the setImageTheme function.


const headerStyles = document.querySelectorAll(
'input[data-drupal-selector^="edit-header-style-options"]'
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script accesses document multiple times. It’s a good practice to minimize global object access by caching elements that you reuse.

}
}

function setImageTheme() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest moving the setImageTheme() function outside the main attach function for clarity and better separation of logic.

@yeniatencio
Copy link
Contributor Author

@yeniatencio I have suggested some suggestions. Please have a look into the revised version and it should work in the same way -

(function (Drupal) {
  "use strict";

  Drupal.behaviors.heroImageTheme = {
    attach: function (context, settings) {
      const imageTheme = context.querySelector(
        'select[data-drupal-selector^="edit-field-landing-page-hero-theme"]'
      );

      if (imageTheme !== null) {
        setImageTheme();

        const headerStyles = context.querySelectorAll(
          'input[data-drupal-selector^="edit-header-style-options"]'
        );

        if (headerStyles.length > 0) {
          headerStyles.forEach((style) => {
            style.addEventListener("change", setImageTheme);
          });
        }
      }

      function setImageTheme() {
        const selectedHeaderStyle = document.querySelector(
          'input[data-drupal-selector^="edit-header-style-options"]:checked'
        );

        if (!selectedHeaderStyle) {
          return; // Exit if no style option is selected.
        }

        let defaultHeaderStyle = selectedHeaderStyle.value;
        imageTheme.value = defaultHeaderStyle === "fullwidth" ? "dark" : "light";
      }
    },
  };
})(Drupal);

Hi @MdNadimHossain , Thanks for the suggestions. I have updated my pr as you requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants