Jump to content

Setting a fields Selectable Pages option via the API


cosmicsafari
 Share

Recommended Posts

Hi all,

I am creating a page field (field of type FieldtypePage) via the API, however im still trying to find some documentation as to how I would go about setting the Selectable Pages for said field using the API.

From what I have found it looks like it involves the use of, albeit this looks like a getter rather than a setter:

$field->getInputfield($page)

Which looks like it would make sense if I wanted to specify the selectable pages by a parent page, but what if I wanted to specify it by say a template?

Link to comment
Share on other sites

So from messing around in the database it looks like the when you manually select a parent under the 'Selectable Pages' section and save, it adds

"parent_id":X,"

to the 'data' cell for the field in questions record within the fields table.

In context

{"tags":"Page-Multi-Select","derefAsPage":0,"collapsed":0,"parent_id":1,"labelFieldName":"title","inputfield":"InputfieldSelect"}

So in theory is there any reason why I can't just update the database directly without going through the API?

Seems hacky but still can't find any examples of being able to do it through the API.

Link to comment
Share on other sites

This was what I ended up at and it seems to work ok even if it is a bit hacky.

$sql = "SELECT data FROM fields WHERE id=:fieldid";
$query = wire('database')->prepare($sql);
$query->bindValue(':fieldid', $f->id);
$query->execute();
$data = $query->fetchAll();

$parts = json_decode($data[0][0],true);
$parts['derefAsPage']='0';
$parts['collapsed'] = '0';
$parts['parent_id'] = $parent->id;
$parts['labelFieldName'] = 'title';
$parts['inputfield'] = 'InputfieldSelect';
$data = json_encode($parts);

$sql = "UPDATE fields SET data=:data WHERE id=:fieldid";
$query = wire('database')->prepare($sql);
$query->bindValue(':data', $data);
$query->bindValue(':fieldid', $f->id);
$query->execute();

 

Link to comment
Share on other sites

I not sure and even didn't test it, but you can try to use 

$field->set('parent_id', '1084');
$field->set('template_ids', '[74]');
$field->set('findPagesSelect', 'test=somevalue');
$field->set('findPagesSelector', 'modified_users_id=40, status=hidden');

 

  • Like 1
Link to comment
Share on other sites

11 hours ago, Zeka said:

I not sure and even didn't test it, but you can try to use 


$field->set('parent_id', '1084');
$field->set('template_ids', '[74]');
$field->set('findPagesSelect', 'test=somevalue');
$field->set('findPagesSelector', 'modified_users_id=40, status=hidden');

 

I tried using  

$f->set('parent_id', $parent->id);

But I get a warning on the page

This field needs to be configured before it can be used.
Link to comment
Share on other sites

public function fieldGroupOverrides($name, $overrides){

        foreach($overrides as $key => $field){

            if (is_array($field)) {

                error_log('==> Override Field: ('.$key.')');
                $t = wire('templates')->get($name);
                $fg = $t->fieldgroup;
                $f = $fg->getField($key, true);

                //All Fields, override label
                if(isset($field['label'])) {
                    $f->label = $field['label'];
                    error_log('===> Override Label Success! ('.$key.') -> ('.$field['label'].')');
                }

                //Page Field, overide selectable pages
                if(isset($field['parent_of_selectable'])){

                    error_log('===> Override Parent of Selectable');

                    $parent = wire('pages')->get('name='.$field['parent_of_selectable']);

			$f->set('parent_id', $parent->id);

                    error_log('===> Override Success! Parent of Selectable = ('.$field['parent_of_selectable'].')');
                }

                wire('fields')->saveFieldgroupContext($f, $fg);

            }

        }
    }

 

Link to comment
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
 Share

×
×
  • Create New...