Skip to content

Commit

Permalink
Prefill tags with most used items and reduce default minimum term len…
Browse files Browse the repository at this point in the history
…gth to 1
  • Loading branch information
HLeithner committed Nov 24, 2020
1 parent 4c4fef0 commit b1c3163
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
10 changes: 9 additions & 1 deletion administrator/components/com_tags/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@
first="1"
last="3"
step="1"
default="3"
default="1"
/>

<field
name="prefill_limit"
type="number"
label="COM_TAGS_CONFIG_TAG_PREFILL_LIMIT_LABEL"
default="30"
filter="integer"
/>

<field
Expand Down
1 change: 1 addition & 0 deletions administrator/language/en-GB/com_tags.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COM_TAGS_CONFIG_SELECTION_SETTINGS_LABEL="Item Selection"
COM_TAGS_CONFIG_SHARED_SETTINGS_DESC="These settings apply to all tag layouts unless they are changed for a specific menu item."
COM_TAGS_CONFIG_SHARED_SETTINGS_LABEL="Shared Layout"
COM_TAGS_CONFIG_TAG_MIN_LENGTH_LABEL="Minimum Search Length"
COM_TAGS_CONFIG_TAG_PREFILL_LIMIT_LABEL="Initial number of shown tags"
COM_TAGS_CONFIG_TAG_SETTINGS_DESC="These settings apply for a Tagged Items List or Compact List of Tagged Items unless they are changed for a specific menu item."
COM_TAGS_CONFIG_TAG_SETTINGS_LABEL="Tagged Items"
COM_TAGS_CONFIG_TAGGED_ITEMS_FIELD_LAYOUT_LABEL="Default Tagged Items Layout"
Expand Down
88 changes: 69 additions & 19 deletions libraries/src/Form/Field/TagField.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function getInput()
$data['options'] = $this->getOptions();
$data['isNested'] = $this->isNested();
$data['allowCustom'] = $this->allowCustom();
$data['minTermLength'] = (int) $this->comParams->get('min_term_length', 3);
$data['minTermLength'] = (int) $this->comParams->get('min_term_length', 1);

return $this->getRenderer($this->layout)->render($data);
}
Expand All @@ -130,12 +130,11 @@ protected function getOptions()
$published = $this->element['published'] ?: array(0, 1);
$app = Factory::getApplication();
$language = null;
$options = [];

// Return only basic options, everything else will be searched via AJAX
if ($this->isRemoteSearch() && !$this->value)
{
return parent::getOptions();
}
// This limit is only used with isRemoteSearch
$limit = (int) $this->comParams->get('prefill_limit', 30);
$isRemoteSearch = $this->isRemoteSearch();

$db = Factory::getDbo();
$query = $db->getQuery(true)
Expand Down Expand Up @@ -184,12 +183,6 @@ protected function getOptions()

$query->where($db->quoteName('a.lft') . ' > 0');

// Preload only active values, everything else will be searched via AJAX
if ($this->isRemoteSearch() && $this->value)
{
$query->whereIn($db->quoteName('a.id'), $this->value);
}

// Filter on the published state
if (is_numeric($published))
{
Expand All @@ -205,16 +198,73 @@ protected function getOptions()

$query->order($db->quoteName('a.lft') . ' ASC');

// Get the options.
$db->setQuery($query);

try
// Preload only active values and 30 most used tags or fill up
if ($isRemoteSearch)
{
$options = $db->loadObjectList();
// Load the most $limit used tags
$topQuery = $db->getQuery(true)
->select($db->quoteName('tag_id'))
->from($db->quoteName('#__contentitem_tag_map'))
->group($db->quoteName('tag_id'))
->order('count(*)')
->setLimit($limit);

$db->setQuery($topQuery);
$topIds = $db->loadColumn();

// Merge the used values into the most used tags
if (!empty($this->value) && is_array($this->value))
{
$topIds = array_merge($topIds, $this->value);
$topIds = array_keys(array_flip($topIds));
}

// Set the default limit for the main query
$query->setLimit($limit);

if (!empty($topIds))
{
// Filter the ids to the most used tags and the selected tags
$preQuery = clone $query;
$preQuery->whereIn($db->quoteName('a.id'), $topIds);

$db->setQuery($preQuery);

try
{
$options = $db->loadObjectList();
}
catch (\RuntimeException $e)
{
return array();
}

// Limit the main query to the missing amount of tags
$count = count($options);
$limit = $limit - $count;
$query->setLimit($limit);

// Exclude the already loaded tags from the main query
if ($count > 0) {
$query->whereNotIn($db->quoteName('a.id'), ArrayHelper::getColumn($options, 'value'));
}
}
}
catch (\RuntimeException $e)

// Only execute the query if we need more tags not already loaded by the $preQuery query
if (!$isRemoteSearch || $limit > 0)
{
return array();
// Get the options.
$db->setQuery($query);

try
{
$options = array_merge($options, $db->loadObjectList());
}
catch (\RuntimeException $e)
{
return array();
}
}

// Block the possibility to set a tag as it own parent
Expand Down
2 changes: 1 addition & 1 deletion libraries/src/HTML/Helpers/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function ajaxfield($selector = '#jform_tags', $allowCustom = true)
{
// Get the component parameters
$params = ComponentHelper::getParams('com_tags');
$minTermLength = (int) $params->get('min_term_length', 3);
$minTermLength = (int) $params->get('min_term_length', 1);

Text::script('JGLOBAL_KEEP_TYPING');
Text::script('JGLOBAL_LOOKING_FOR');
Expand Down

0 comments on commit b1c3163

Please sign in to comment.