Jump to content

How to addFlag to a field in a fieldgroup context?


donatas
 Share

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

×
×
  • Create New...