Jump to content
mel47

Selector to exclude categories not used - solved

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

 

Share this post


Link to post
Share on other sites

Do these unused categories also have an empty pageref to categ? 

A selector like: "template=activity, pageref.count>0" will only return activities with filled page reference to categ. See more here: https://processwire.com/docs/selectors/#count

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

There are a few solutions and the core in the end even added an selector for the usecase.

  • Like 4

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...