A typical Developer Blog
by Gordon Franke
Icon

How can i use field credentials for symfony admin generator forms?

When you would restrict the display of a field or fieldset group on user credential. you must do the following

1. open the action class and add

public function preExecute()
{
  parent::preExecute();
 
  $this->configuration->setUser($this->getUser());
}

2. open the *GeneratorConfiguration class and add

protected $user;
 
public function setUser($user)
{
  $this->user = $user;
}
 
public function getUser()
{
  return $this->user;
}
 
public function getEditDisplay()
{
  $fieldsets = parent::getEditDisplay();
  if (!$this->getUser()->hasCredential('admin'))
  {
    unset(
      $fieldsets['Auth'][2], // field
      $fieldsets['Rights']   // field group
    );
  }
 
  return $fieldsets;
}
 
public function getNewDisplay()
{
  ... // see getEditDisplay
}
 
public function getFormOptions()
{
  return array('user' => $this->getUser());
}

3. last remove the fields from the form, open the *Form class and add

public function configure()
{
  ...
  if (!$this->getOption('user')->hasCredential('admin'))
  {
    unset($this['is_super_admin'], $this['groups_list'], $this['permissions_list']);
  }
  ...
}

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Author:

Category: symfony

Tagged: , , , ,

2 Responses

  1. Thank you! This article has been helpful for me. I was just looking for how to pass a parameter from admin-generator to form

  2. Adam says:

    Thanks for this info, it took a little digging to realise that the GeneratorConfiguration class you are talking about is actually in:

    /apps//modules//lib/GeneratorConfiguration.class.php

    but that was my fault for not RTFM
    :)

    Your code helped with my project :D

Leave a Reply

CommentLuv badge