Jump to content
jploch

[SOLVED] create field and add to template

Recommended Posts

Hey there,
Iam working on a module (that extends FieldtypePageTable) that needs some fields to work. I want to create those fields and add them to a template automatically via api. To create and add the fields I use the "Pages::added" hook. I only want to add the field if the page that is created also has a "FieldtypePageTableExtendedGrid" field. I have a working example that uses a hard coded template name to check for the right template, but this approach would need the user to stick to the naming, wich I would like to avoid. Here is my code so far:

$this->addHookAfter('Pages::added', function(HookEvent $event) {
        $page = $event->arguments(0);
        $fields = wire('fields');
         
        if($page->template == 'main_grid') { // would like to avoid hard coded template name

     	 if(!$fields->get('pgrid_settings_style_large')) {
            $field = new Field;
			$field->type = $this->modules->get("FieldtypeTextarea");
			$field->name = "pgrid_settings_style_large";
			$field->label = $this->_("Style Large");
			$field->tags = 'pgrid';
			$field->save();
          }
            
            if(!$page->hasField('pgrid_settings_style_large')) {
            $field = $fields->get('pgrid_settings_style_large');
            $template = $page->template;
            $template->fieldgroup->add($field);
            $template->fieldgroup->save();
          
              }
    
    }
});

 Maybe I need another hook? I tried a before "InputfieldPageTable::render" hook with no success. Any help would be appreciated!

Share this post


Link to post
Share on other sites

I found a post on how to check if a field type is present. Now it seems to work great:

 // add style fields
       $this->addHookAfter('Pages::added', function(HookEvent $event) {
        $page = $event->arguments(0);
        $fields = wire('fields');
         
         // add fields only if FieldtypePageTableExtendedGrid is inside template
         foreach ( $page->fields as $field ) {

		if ( $field->type instanceof FieldtypePageTableExtendedGrid ) {
		 if(!$fields->get('pgrid_settings_style_large')) {
            $field = new Field;
			$field->type = $this->modules->get("FieldtypeTextarea");
			$field->name = "pgrid_settings_style_large";
			$field->label = $this->_("Style Large");
			$field->tags = 'pgrid';
			$field->save();
          }
            
            if(!$page->hasField('pgrid_settings_style_large')) {
            $field = $fields->get('pgrid_settings_style_large');
            $template = $page->template;
            $template->fieldgroup->add($field);
            $template->fieldgroup->save();
              }
		}

	} 
         
});

 

  • Like 2

Share this post


Link to post
Share on other sites

Small remarks. You should break the loop when work is done:

foreach ($page->fields as $field) {
    if ( $field->type instanceof FieldtypePageTableExtendedGrid ) {
        // ...
        break;
    }
}

Another approach without loop. Fields is an instance of WireSaveableItems providing a find($selector) function:

$selector = implode('|',$fields->find('type=FieldtypePageTableExtendedGrid')->each('name'));
if ($page->hasField($selector)) {
    // ... do something
}

 

  • Like 2

Share this post


Link to post
Share on other sites
13 hours ago, kixe said:

You should break the loop when work is done:


foreach ($page->fields as $field) {
    if ( $field->type instanceof FieldtypePageTableExtendedGrid ) {
        // ...
        break;
    }
}

This assumes only one field of type FieltypePageTableExtendedGrid is present on the page, no? 😄 

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, kongondo said:

This assumes only one field of type FieltypePageTableExtendedGrid is present on the page, no? 😄 

If minimum one field is present in the template the other field should be created/ added. No need to go on with the loop.

  • Like 1

Share this post


Link to post
Share on other sites
45 minutes ago, kixe said:

If minimum one field is present in the template the other field should be created/ added. No need to go on with the loop.

Of course, you are right. I should have read the requirements more carefully :-)

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...