This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

kdoon80
kdoon80

I'm trying to pass a simple value to a component. Based on the document, I should be able to pass a variable by overriding the property value. I'm calling the component from a page, and I want to be able to pass the variable to the php file in a component. (https://octobercms.com/docs/cms/components#variables)

{% component 'demoTodoAlias' maxItems='7' %}

I've tried similar method as above, but somehow the variable does NOT get passed. Any ideas?

In my page I have this

{% component 'cName' catId='7' %}

In my component I have the property as:

public function defineProperties()
{
    return [
        'catId' => [
            'description' => 'xxxxxxxxxxx',
            'title'       => 'xxxxxxxxxx',
            'default'     => '0',
            'type'        => 'string'
        ]
    ];
}

in the init() function of component I tried to access it with the following two but neither works.

$this->property('catId');

and

$this->param('catId');

and I have updated octobercms to the stable version, and everything is up-to-date.

thanks,

Last updated

axomat
axomat

Why are you using the init() function? I seem to remember that init does not have access to the component property values. You should use onRun as the first point at which they are available.

Actually I posted about this some time ago. I would like to see init be able to do more things such as permissions checking so that it does not have to be done on every entry to the component. I am guessing this is your problem as well.

kdoon80
kdoon80

axomat said:

Why are you using the init() function? I seem to remember that init does not have access to the component property values. You should use onRun as the first point at which they are available.

Actually I posted about this some time ago. I would like to see init be able to do more things such as permissions checking so that it does not have to be done on every entry to the component. I am guessing this is your problem as well.

That didn't seem to have fixed the issue. Thanks

axomat
axomat

Do mean that

$this->property('catId');

does not contain 7 or is not set at all when you use it in onRun

kdoon80
kdoon80

I can get the property value a) if it was set as default in the component php file b) if set in the component GUI box in the parameter when setting the property in the actual page (i.e. initializing the component to the page)

But I can't pass a variable dynamically to the component, which that's all I'm trying to accomplish. I read in the documents that I can pass variables dynamically in three ways to a component 1) set {{variable}} as the parameter to the property when initializing the component to the page 2) set URL parameter as :parameter to the property when initializing the component to the page 3) override the property when calling the component - this is what I'm trying to do with no luck.

so to answer your question, i'm never getting 7 when trying to pass it. It either picks up the default value, or none at all if no default is set or if I'm not setting the property value when initializing the component.

Last updated

kdoon80
kdoon80

I just found a workaround!!!! I removed the component from my page, instead I created a partial, then passed the variable to the partial. Then initialized the component inside the partial, and passed {{variable}} to the component parameter inside the partial! This is similar to utilizing the #1 method I posted in my previous message. I still can't get the #3 method to work!!

axomat
axomat

I found this :

These properties will not be available in the onRun() method since they are established after the page cycle has completed. Instead they can be processed by overriding the onRender() method in the component class. The CMS controller executes this method before the default markup is rendered.

Last updated

kdoon80
kdoon80

axomat said:

I found this :

These properties will not be available in the onRun() method since they are established after the page cycle has completed. Instead they can be processed by overriding the onRender() method in the component class. The CMS controller executes this method before the default markup is rendered.

Awesome man :) onRender() did the trick. Thanks a bunch for the help.

adilshahzad8217110
adilshahzad8217110

It's also possible to override the value using below piece of code onRun() method

$this->page->components['my-component-name']->setProperty('property-name, $property-value);

paulgrafx
paulgrafx

Hi kdoon80,

Did you manage to use the value set onRender() in other functions in the component?

alexwenzel
alexwenzel

to use a composed component property I did the following:

function onInit()
{
$this->page->components['faq']->setProperty('slug', "faq-country-" . $this->param('id'));
}

1-11 of 11

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.