Collection in static function used by Admin blueprint does not contain pages

Hello!
I have made a static function to retrieve some attribute from all the children from a page path, and also another one filtering all pages by type. None work. Neither count($pages) or foreach() through the collection seems to work.

Anyone can point me to what I’m doing wrong or what ca be happening??

The functions:

public static function getPageTitle($collection_options, $path)
	{

		$grav = Grav::instance();
		$l = $grav['log'];
		$l->info('Tutorial');
		$page = $grav['page'];
		$collection = $page->evaluate([$collection_options => $path]);
		$titles = [];

		foreach ($collection as $item) {
			$titles[$item->title()] = $item->title();
		}
		$l->info('Count: '.count($collection));
		$l->critical(print_r($titles, true));

		return $titles;
	}

This is called from a blueprint with :

      fields:
        content:
          fields:
            llocs:
              type: select
              label: Municipis
              data-options@: ['\Grav\Theme\Espurnes::getPageTitle', '@page.modular', '/qui-som/territori-barroc']

This is the entire theme file, in case it helps. And here you will see also the static function municipis() that tries the approach of returning the data without variables.

<?php
namespace Grav\Theme;

use Grav\Common\Grav;
use Grav\Common\Theme;

class Espurnes extends Theme
{
  public static function getSubscribedEvents()
  {
      return [
          'onThemeInitialized'    => ['onThemeInitialized', 0],
          'onTwigLoader'          => ['onTwigLoader', 0],
          'onTwigInitialized'     => ['onTwigInitialized', 0],
      ];
  }

  public function onThemeInitialized()
  {

  }

  // Add images to twig template paths to allow inclusion of SVG files
  public function onTwigLoader()
  {
      $theme_paths = Grav::instance()['locator']->findResources('theme://images');
      foreach($theme_paths as $images_path) {
          $this->grav['twig']->addPath($images_path, 'images');
      }
  }

  public function onTwigInitialized()
  {
      $twig = $this->grav['twig'];

      $form_class_variables = [
//            'form_outer_classes' => 'form-horizontal',
          'form_button_outer_classes' => 'button-wrapper',
          'form_button_classes' => 'btn',
          'form_errors_classes' => '',
          'form_field_outer_classes' => 'form-group',
          'form_field_outer_label_classes' => 'form-label-wrapper',
          'form_field_label_classes' => 'form-label',
//            'form_field_outer_data_classes' => 'col-9',
          'form_field_input_classes' => 'form-input',
          'form_field_textarea_classes' => 'form-input',
          'form_field_select_classes' => 'form-select',
          'form_field_radio_classes' => 'form-radio',
          'form_field_checkbox_classes' => 'form-checkbox',
      ];

      $twig->twig_vars = array_merge($twig->twig_vars, $form_class_variables);

  }

	public static function municipis()
	{
		$grav = Grav::instance();
		$pages = $grav['pages']->all()->ofType('espai');
		$collection = $pages->getCollection([
			'items' => [ '@page.children.visible' => '/qui-som/territori-barroc' ],
			'order' => ['by'=> 'lloc', 'dir'=> 'asc']
		]);
		$logger = $grav['log'];
		$pages = $grav['page']->children();
		$logger->info('Children?');
		$logger->info('Count: '.count($pages));

		$dump = (object)['something'];
		$llocs = [];
		foreach ($pages as $page) {
			$dump[]=['something'=>$page->header()];
			array_push($llocs, $page->header()->lloc);
				/*$llocs[] = $page->header->lloc;
				$header = $page->header();
				if (isset($header->lloc)) {
					$llocs[] = $header->lloc;
				}*/
		}

		$logger->critical('DUMP: ' . print_r($dump, true));
		$logger->critical('LLOCS: ' . print_r($llocs, true));

		return $llocs;
	}

	public static function getPageTitle($collection_options, $path)
	{

		$grav = Grav::instance();
		$l = $grav['log'];
		$l->info('Tutorial');
		$page = $grav['page'];
		$collection = $page->evaluate([$collection_options => $path]);
		$titles = [];

		foreach ($collection as $item) {
			$titles[$item->title()] = $item->title();
		}
		$l->info('Count: '.count($collection));
		$l->critical(print_r($titles, true));

		return $titles;
	}
}

1 Like

@Johanan, Try adding the following:

$grav = Grav::instance();

/** @var Admin */
$admin = $grav['admin'];
$admin->enablePages(); 

To improve the performance of Admin, pages are no longer initialised by default. Hence $grav['pages']->all() will be empty.

1 Like

Amazing! Thank you!

But now I have a doubt. If I have an attribute called lloc in header, should I be able to access to that data with:

$page->header()->lloc

Grav crashes with the error: An exception has been thrown during the rendering of a template (“Undefined property: stdClass::$lloc”).

@Johanan, It seems your initial question has been solved. if so, please mark this topic as being solved.

Please note, it is a bad practice to add/burry multiple questions to a single topic. Please create a new topic.

1 Like

OK. Sorry… And thank you!!