Jump to content

Selector to exclude categories not used - solved


mel47
 Share

Recommended Posts

Hi,

I have difficulty to find the good selector.
Home
 ----Categories
 ------------categ
-----Activities
----------activity (with a pageref to categ)

I create a search form with multichoice to listing all categories ($categ = $pages->find("parent.id=1057, sort=title")). However, some categories are currently not used and I don't want them to be listed.
How I can modify my selector the more efficient way? I was thinking of searching categories in all activity's pages and recreate the array. Or exclude pages category without references to them. Or something else simpler I don't know??

Thanks

Mel

 

Link to comment
Share on other sites

Here is a hook method for /site/ready.php that gets the pages that are selected in a given Page Reference field:

/**
 * Get pages selected in a Page Reference field
 * Use optional argument $options for limiting 'template' and/or 'parent_id', and to 'include_unpublished'.
 * Usage: $selected_pages = $fields->my_page_field->getSelectedPages(['template' => 'basic_page']);
 * The number of times each page is selected is available in a custom "occurrunces" property on each selected page.
 */
$wire->addHookMethod('Field(type=FieldtypePage)::getSelectedPages', function(HookEvent $event) {
	/* @var Field $field */
	$field = $event->object;
	$options = $event->arguments(0);
	$get_options = [];
	if(isset($options['template'])) $get_options['template'] = $this->wire('templates')->get($options['template']);
	if(isset($options['parent_id'])) $get_options['parent_id'] = (int) $options['parent_id'];

	$table = $field->getTable();
	$query = $this->wire('database')->query("SELECT data FROM $table");
	$ids = $query->fetchAll(\PDO::FETCH_COLUMN);
	$count_values = array_count_values($ids);
	$items = $this->wire('pages')->getById(array_keys($count_values), $get_options);
	foreach($items as $item) {
		if(empty($options['include_unpublished']) && $item->status > Page::statusUnpublished) {
			$items->remove($item);
		} else {
			$item->occurrences = $count_values[$item->id];
		}
	}
	$event->return = $items;
});

The following is an example of how you would get the category pages that are selected in your category field. The number of times each category is selected is available in the custom "occurrences" property if you need it.

// Get the selected category pages
$selected_categories = $fields->categories->getSelectedPages();

// Demo output
foreach($selected_categories as $selected_category) {
    echo "<p>Category {$selected_category->title} is selected in {$selected_category->occurrences} page(s).</p>";
}

 

  • Like 2
Link to comment
Share on other sites

Thanks everyone, but especially @LostKobrakai. This "owner" selector is wonderful!

So for the sake of people searching on forum, it give this so simple selector:

$CategUsed = $pages->find('categ.owner.template=activity');

Mel

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...