Problem/Motivation

  1. The SiteSettingsLoader class does not provide methods to load the site setting entities.
  2. Refactor the class in general.

Proposed resolution

Add methods to load the site setting entities. Refactor the class.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marco-s created an issue. See original summary.

marco-s’s picture

Patch #2 includes:

  • New methods loadAllEntities(), loadEntitiesByType(), doesEntityOfTypeExists()
  • New class SiteSettingsCache(). The cache logic from SiteSettingsLoader class has been extracted to this class
  • Some refactoring
marco-s’s picture

Hm, the second patch upload did not override my first added patch. Here is the right patch.

orlando.thoeny’s picture

Works for me.
We should have a look at the tests and fix them.

marco-s’s picture

Status: Active » Needs review
FileSize
13.56 KB
583 bytes

Forgot a variable name replacement. Fixed in this patch.

scott_euser’s picture

Thanks for the work on this Marco-s!

+++ b/src/SiteSettingsLoader.php
@@ -43,19 +29,89 @@ class SiteSettingsLoader {
+  /**
+   * Checks if a site setting entity exists for the given type.
+   *
+   * @param string $type
+   *   The site setting type.
+   *
+   * @return bool
+   *   TRUE if exists, FALSE otherwise.
+   *
+   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+   */
+  public function doesEntityOfTypeExists($type) {
+    return !empty($this->loadEntitiesByType($type));
   }

I couldn't see where this is used, is this specific to a use case you have? Not against it, just wanting to understand the goal.

For the rest, it seems to work fine, I've tested it out on a couple larger sites and haven't been able to find any issues.

marco-s’s picture

Hi scott_euser

Yes, we have use cases for doesEntityOfTypeExists(). For example if you reference a site settings type in code or in config. In such a case, you want to check first if the site setting entity exists (e.g. before executing further steps).

marco-s’s picture

I cast the changes into a merge request !1

marco-s’s picture

We sometimes have problems with a website that has a lot of traffic. Site settings causes Call to a member function isTranslatable() on null in SqlContentEntityStorage during drush cr, because $definitions is empty (see also 2928108). We have not yet been able to find the real cause. But I try to reduce the EntityTypeManager loadMultiple() calls. With this change ( f560b762) I reduce the calls in our case from 48 calls to just 2.

id.conky’s picture

FileSize
12.37 KB

Hi!
I also think that settings loader class should be refactored for usability and performance purposes.
You guys have a good ideas, but for my needs I decided to do it in other, simpler manner - by moving all settings to be stored in Drupal static cache.

Patch with changes attached.

scott_euser’s picture

Status: Needs review » Needs work

Thanks for your work on this id.conky and marco-s. Apologies for the silence for so long, having a child has reduced my free time a bit :)

id.conky, I think we need a combination of your approach; I understand the static cache will further improve performance within a single page load. However it removes the benefits of the cache across page loads. Site Settings rarely change in most use cases and the loader as is caches a much lighter version of the data compared to full entities (which has its downsides as well of course). I would prefer to see your use of drupal_static added in to marco-s' work on the SiteSettingsCache class.

scott_euser’s picture

FYI progress on new loader that works more like standard Drupal rendering happening here https://www.drupal.org/project/site_settings/issues/3395787

Does not stop refactoring this loader as long as output is maintained as is for this loader to avoid breaking all the existing sites that use this module.

scott_euser’s picture

Status: Needs work » Closed (outdated)

Things have moved on significantly in the 2x branch and major refactor has occurred. Thanks all for the work on this!

scott_euser’s picture

Status: Closed (outdated) » Fixed
scott_euser’s picture

Changed to fixed instead to recognise the contribution and give credit as this

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.