2

Is it possible to access the properties of a component e.g. blogPosts in the OnStart() function? If so, how?

title = "Blog Category"
url = "/blog/category/:slug/:page?"

[blogPosts]
categoryFilter = ":slug"
postsPerPage = 3
==
function onStart()
{
    // Access categoryFilter property
    $catfilter = ???
    ...
}
==
creg
  • 154
  • 3
  • 13
  • Don't understand what you asking. Be more clear on your question. – Kazmi Jul 19 '19 at 15:57
  • You see the OnStart() function. I want to read the value of the categoryFilter property there. This property was set outside the function in the properties of the component blogPosts. Put simply, I'm looking for such a solution $catfilter = categoryFilter. Unfortunately, categoryFilter cannot be accessed in this way. – creg Jul 19 '19 at 16:16
  • I thought I could access the properties as follows: this.page.components['blogPosts'].properties['postsPerPage']. But this only works outside the function within a twig, e.g. {{ dump(this.page.components['blogPosts'].properties['postsPerPage']) }}. I think I should have another beer or two. – creg Jul 19 '19 at 16:33
  • @creg Answered your question. – Pettis Brandon Jul 24 '19 at 17:45

1 Answers1

0

I got to ask why do you need to access the component properties?

You can access the parameter ':slug' $this->param('slug');.

This should work for you to get the array of properties.

function onStart() {
    $this['properties'] = collect($this->page->components['blogPosts'])->last()['postsPerPage'];
}

I am adding another way to get the properties from the array. I am assuming that properties is probably always last but if it isn't:

    $array = collect($this->page->components['blogPosts'])->toArray();
    $properties = [];
    foreach ( $array as $key => $property) {
        if ( strpos($key, 'properties') !== false ) {
            $properties[$key] = $property;
        }
    }
    $this['properties'] = $properties['postsPerPage'];
Pettis Brandon
  • 875
  • 1
  • 6
  • 8