Jump to content

How to addFlag to a field in a fieldgroup context?


Recommended Posts

Hi,
Could someone please direct me to how should I go about using `addFlag` on a field in a fieldgroup context?

Setting contexts through API is pretty new to me, however I managed to set all other necessary settings through `setFieldContextArray()` function. But the access flags are only applied through `$field->addFlag(128)` (when not inside fieldgroup context) - https://processwire.com/api/ref/field/add-flag/

I have tried (in a module install function):

// Create a new fieldgroup
$fieldGroup = new Fieldgroup();
$fieldGroup->name = static::NAME;
$fieldGroup->add('title'); // trying to set flag to this 'title'
// ...Adding other few fresh fields to fieldgroup
$fieldGroup->save(); // Works fine.

$field_title = wire('fields')->get('title');
$fieldGroup->setFieldContextArray( $field_title->id, array(
  'columnWidth' => 50,
  'useRoles' => true,
  'accessFlags' => [64,128], // Doesn't work.
  'addFlag' => 128, // Doesn't work.
));
//$fieldGroup->getField('title',true)->addFlag(128)->save(); // Doesn't work with field->save() nor without.
$fieldGroup->saveContext(); // Works fine with above setFieldContextArray() settings.

// Adding it to a new template and saving. Works fine.
$template = new Template();
$template->name = static::NAME;
$template->fieldgroups_id = $fieldGroup->get('id');
$template->save();

The API docs seem a bit lacking in this department, very hard to figure out the whole flow 🙂

Any ideas? Thanks in advance!

Link to comment
Share on other sites

/**
 * allowed flag constants (bitmask) for context
 * @see core/field.php
 *
 */
Field::flagAccess // 32
Field::flagAccessAPI // 64
Field::flagAccessEditor // 128

/**
 * set context via fieldgroup
 * @see core/fieldgroup.php
 *
 */
$fieldGroup->setFieldContextArray($field_title->id, ['flagsAdd' => 192, 'flagsDel' => 32]);
$fieldGroup->saveContext();

/**
 * set context via fields
 * @see core/fields.php
 *
 */
$field_title = wire('fields')->get('title');
$field_title->addFlag(128); // runtime
wire('fields')->saveFieldgroupContext($field_title, $fieldGroup);

not tested, but should work

  • Like 1
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Never really got my head around field groups and it shows, In my module uninstall I think I figured out how to delete a page, then a template and it's fieldgroup so that nothing gets upset. But for fields that belong to templates that need to stoick around I'm still abit stuck to how you would remove the field and when I try I still get:
      Unable to delete field '[field name]' because it is in use by these fieldgroups: 'user'
      is there a tutorial or best method for removing added fields, pages and templates?
    • By abdus
      Based on this forum post, I wanted to find a more elegant solution for changing a field's settings per template. Hooking the page editor after it generates the form works and may still be needed for more complex modifications. But using field & template context, it's easier to modify the field settings and it greatly reduces the need for creating an almost identical field just to adjust a few things.
      In my blog post, I've written on how to extend contextual options to allow any fieldtype / inputfield settings to be changed depending on the template. Hope you find it useful, and if you have any questions or comments, feel free to post them here.
      https://abdus.co/blog/doing-more-with-fewer-fields-in-processwire/
    • By abdus
      I'm trying to add a new option to InputfieldTextarea. Depending on that option, I want to change how the input is rendered. I also want to change this option depending on different templates and repeaters, meaning it can have different values for different fieldgroups.
      I hooked into three methods:
      $this->addHookBefore('InputfieldTextarea::render', $this, 'hookInputRender'); $this->addHookAfter('InputfieldTextarea::getConfigInputfields', $this, 'hookInputSettings'); $this->addHookAfter('InputfieldTextarea::getConfigAllowContext', $this, 'hookInputContext'); In hookInputSettings, I build the additional option
      protected function hookInputSettings(HookEvent $e) { /** @var InputfieldTextarea $field */ $wrapper = $e->return; $field = $e->object; /** @var InputfieldSelect $font */ $font = $this->modules->get('InputfieldSelect'); $font->label = $this->_('Font'); $font->name = 'fontFamily'; $font->addOptions(self::fontOptions); $font->attr('value', $field->fontFamily); $wrapper->add($font); $e->return = $wrapper; } It shows up in field settings with no problem

      When I pick an option and save, it even shows up in the database. However, I cannot get the properties of that field in that fieldgroup context. Most other inputfields can get their inputfield settings because are inside a class that extends Inputfield, so $this->myOption works.
      If I hook into FieldtypeTextarea::getConfigInputfields, it works too, because getConfigInputfields method is called with $this as its argument, inside hooks it's possible to access fieldgroup specific settings. But for Inputfield, it's not given any arguments, so hooking Inputfield::getConfigInputfields, you won't be able to get any information about the context.
      // /wire/core/Field.php public function ___getConfigInputfields() { // ... if(!$fieldgroupContext || count($allowContext)) { // ... try { $fieldtypeInputfields = $this->type->getConfigInputfields($this); // ... } // ... } $inputfields = $this->wire(new InputfieldWrapper()); // ... if($inputfield) { if($fieldgroupContext) { $allowContext = array('visibility', 'collapsed', 'columnWidth', 'required', 'requiredIf', 'showIf'); $allowContext = array_merge($allowContext, $inputfield->getConfigAllowContext($this)); } // ... $inputfieldInputfields = $inputfield->getConfigInputfields(); // ... } // ... } So, as a solution I gave it $this as a parameter
      // $inputfieldInputfields = $inputfield->getConfigInputfields(); $inputfieldInputfields = $inputfield->getConfigInputfields($this); Now everything works without any hacks. It works in field settings, templates, repeaters just fine.
      protected function hookInputSettings(HookEvent $e) { // ... $field = $e->arguments(0); // ... $font->attr('value', $field->fontFamily); // WORKS! // ... } I wanted to write this post because it drove me mad last night. I guess the next step is to make a pull request.
    • By cosmicsafari
      Hi all,
      Just another quick question, how would I select a previously created field group so that I can add it to a template using the API?
      All the examples I have seen thus far are creating the field group and adding it to a template at that point, I would rather create all my field groups beforehand and then assign them to their templates in batch.
    • By cosmicsafari
      Hi all,
      Just a quick question before I jump in and inevitably break the module I am working on.
      Is it possible to assign multiple field groups to a single template via the api?
      I am creating a whole bunch of fields, templates and pages on the fly at install and being able to assign multiple field groups to a template would be quite advantageous.
×
×
  • Create New...