private function CshsOptionsFromHelper::getOptions in Client-side Hierarchical Select 8.3
Same name and namespace in other branches
- 8 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::getOptions()
- 8.2 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::getOptions()
Returns the list of options for `cshs` element.
Parameters
int $parent: The ID of a parent term to start load children of.
int|null $max_depth: The number of levels of the tree to return.
Return value
\Drupal\cshs\Component\CshsOption[] Widget options.
2 calls to CshsOptionsFromHelper::getOptions()
- CshsOptionsFromHelper::formElement in src/
CshsOptionsFromHelper.php - Returns the form for a single widget.
- CshsOptionsFromHelper::settingsForm in src/
CshsOptionsFromHelper.php - Returns a form to configure settings.
File
- src/
CshsOptionsFromHelper.php, line 325
Class
- CshsOptionsFromHelper
- Defines a class for getting options for a cshs form element from vocabulary.
Namespace
Drupal\cshsCode
private function getOptions(int $parent = 0, int $max_depth = NULL) : array {
$cache =& \drupal_static(__METHOD__, []);
$cache_id = "{$parent}:{$max_depth}:" . \implode('-', $this
->getVocabulariesIds());
if (!isset($cache[$cache_id])) {
$storage = $this
->getTermStorage();
$cache[$cache_id] = [];
if ($this
->needsTranslatedContent()) {
$get_name = function (object $term) use ($storage) : string {
return $this
->getTranslationFromContext($storage
->load($term->tid))
->label();
};
}
else {
// Avoid loading the entity if we don't need its specific translation.
$get_name = static function (object $term) : string {
return $term->name;
};
}
foreach ($this
->getVocabularies() as $vocabulary) {
// Historically vocabulary labels are not translatable
// and the `t()` trick should be applied.
$group = $this
->getTranslatedValue($vocabulary
->label());
foreach ($storage
->loadTree($vocabulary
->id(), $parent, $max_depth, FALSE) as $term) {
\assert($term instanceof \stdClass);
\assert(\is_array($term->parents));
\assert(\is_numeric($term->status));
\assert(\is_numeric($term->depth));
\assert(\is_numeric($term->tid));
\assert(\is_string($term->name));
// Allow only published terms.
if ((bool) $term->status) {
// The `parents` always has a value. In case there are no parents
// the value is `['0']`. Check for an empty value just in case.
$parent_tid = (string) \reset($term->parents) ?: '0';
$cache[$cache_id][$term->tid] = new CshsOption($get_name($term), $parent_tid > 0 ? $parent_tid : NULL, $group);
}
}
}
}
return $cache[$cache_id];
}