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

ay57808
ay57808

Basically I need to generate pages automatically based on what the user fills in a form, the problem is I can't figure out how to attach a component and fill a property in the component with a variable.

According to https://octobertricks.com/tricks/create-cms-page-with-translation-from-php-code I only need to do this

$page = new \Cms\Classes\Page([
        'fileName' => $this->slug,
        'title' => $this->name,
        'url' => '/' . $this->slug,
        'layout' => 'mylayout'
]);
$page->settings['components']['mycomponent']['variable_id'] = $this->id;

The page is created, but I never get the component attached, this is what shows in the .htm file:

title = "Page title"
url = "/pageslug"
layout = "mylayout"
==

I have also tried the following:

      $page->components[] = [
           'mycomponent' => [
              'variable_id' => $this->id
          ]
      ];

But no luck... I should note, that I'm inside a model, in a function that is called after the model is saved. What am I doing wrong? Is this even possible? I considered making a function to directly write the component on the .htm files, but that will be my last resort.

mjauvin
mjauvin
$page->settings['components']['myComponent'] = ['variable_id' => $this->id] ;
ay57808
ay57808

mjauvin said:

$page->settings['components']['myComponent'] = ['variable_id' => $this->id] ;

Didn't work either, the page is still created without the component. I ended up using php to write to the *.htm file directly and place the component. Which works, but it's not the ideal way.

mjauvin
mjauvin

that's odd, it's working fine for me. Did you try to assign just an empty array like this?

$page->settings['components']['myComponent']  = [] ;
mjauvin
mjauvin

Show your actual code.

mjauvin
mjauvin

No, you're right, this does not get saved with the page... I'm sure this was working before when I wrote the trick. Let me investigate...

mjauvin
mjauvin

Looks like the correct syntax is this:

    $page->attributes['myComponent'] = ['variable_id' => $this->id];

I've update the page on OctoberTricks.

Sorry for the confusion, I really thought I had properly tested this before.

ay57808
ay57808

mjauvin said:

Looks like the correct syntax is this:

   $page->attributes['myComponent'] = ['variable_id' => $this->id];

I've update the page on OctoberTricks.

Sorry for the confusion, I really thought I had properly tested this before.

It is working! Thanks for the help, really appreciate it.

1-8 of 8

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