Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created September 21, 2018 06:35
Show Gist options
  • Save sminnee/bec52b2d0f50a42f44a0aea2a05bc2af to your computer and use it in GitHub Desktop.
Save sminnee/bec52b2d0f50a42f44a0aea2a05bc2af to your computer and use it in GitHub Desktop.
commit 01b2e3a0f8911c82e5072b2016e525e1ed1ab30a
Author: Sam Minnee <sam@silverstripe.com>
Date: Fri Sep 21 18:34:47 2018 +1200
FIX: Use Hierarchy::prepopulateTreeDataCache() in fluent
Requires https://github.com/silverstripe/silverstripe-framework/pull/8395
diff --git a/src/Extension/FluentVersionedExtension.php b/src/Extension/FluentVersionedExtension.php
index 5838f10..97c8eba 100644
--- a/src/Extension/FluentVersionedExtension.php
+++ b/src/Extension/FluentVersionedExtension.php
@@ -75,10 +75,13 @@ class FluentVersionedExtension extends FluentExtension
protected static $idsInLocaleCache = [];
/**
+ * Used to enable or disable the prepopulation of the locale content cache
+ * Defaults to true.
+ *
* @config
- * @var array
+ * @var boolean
*/
- private static $prepopulate_locale_object_types = [];
+ private static $prepopulate_localecontent_cache = true;
protected function augmentDatabaseDontRequire($localisedTable)
{
@@ -317,12 +320,6 @@ class FluentVersionedExtension extends FluentExtension
}
}
- $ownerClass = get_class($this->owner);
- $prepopulateClasses = Config::inst()->get(self::class, 'prepopulate_locale_object_types');
- if (is_array($prepopulateClasses) && in_array($ownerClass, $prepopulateClasses)) {
- self::prepoulateIdsInLocale($locale, $ownerClass);
- }
-
// Get table
$baseTable = $this->owner->baseTable();
$table = $this->getLocalisedTable($baseTable);
@@ -362,7 +359,31 @@ class FluentVersionedExtension extends FluentExtension
}
/**
- * Prepopulate the cache of
+ * Hook into {@link Hierarchy::prepopulateTreeDataCache}.
+ *
+ * @param DataList $recordList The list of records to prepopulate caches for. Null for all records.
+ * @param array $options A map of hints about what should be cached. "numChildrenMethod" and
+ * "childrenMethod" are allowed keys.
+ */
+ public function onPrepopulateTreeDataCache(DataList $recordList = null, array $options = [])
+ {
+ if (!Config::inst()->get(static::class, 'prepopulate_localecontent_cache')) {
+ return;
+ }
+
+ // This functionality hasn't been implemented; it's not actually used right now, so assumeYAGNI
+ if ($recordList) {
+ user_error(
+ "FluentVersionedExtension::onPrepopulateTreeDataCache not currently optimised for recordList use",
+ E_USER_WARNING
+ );
+ }
+
+ self::prepoulateIdsInLocale(FluentState::singleton()->getLocale(), $this->owner->baseClass());
+ }
+
+ /**
+ * Prepopulate the cache of IDs in a locale, to optimise batch calls to isLocalisedInStage.
*
* @param string $locale
* @param string $dataObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment