Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

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

Solved by pamtbaau View solution

Started by Johanan Marín 2 years ago · 5 replies · 219 views
2 years ago

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:

PHP
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 :

YAML
      fields:
        content:
          fields:
            llocs:
              type: select
              label: Municipis
              data-options@: ['\Grav\Theme\Espurnes::getPageTitle', '@page.modular', '/qui-som/territori-barroc']
last edited 02/06/24 by Johanan Marín
2 years ago

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
<?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
2 years ago Solution

@Johanan, Try adding the following:

PHP
$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
last edited 02/06/24 by pamtbaau
2 years ago

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:

PHP
$page->header()->lloc

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

2 years ago

@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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 455 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago