PHP code example of vctls / select2entity-extension-bundle
1. Go to this page and download the library: Download vctls/select2entity-extension-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vctls / select2entity-extension-bundle example snippets
namespace App\Util;
use Vctls\Select2EntityExtensionBundle\Util\Searcher;
class ExampleSearcher extends Searcher
{
public $alias = 'example';
public $idMethod = 'getId';
public $textMethod = '__toString';
public $searchfileds = ['example.name'];
}
namespace App\Form\Example;
use App\Entity\Example;
use Vctls\Select2EntityExtensionBundle\Util\S2;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ExampleType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// with the helper class...
$builder->add(...S2::build('example', Example::class));
// or without it
$builder->add('example2', Select2EntityType::class, [
'remote_route' => 'generic_autocomplete',
'remote_params' => [
'classname' => Example::class
],
'class' => Example::class,
'primary_key' => 'id',
'text_property' => 'name',
'minimum_input_length' => 2,
'delay' => 250,
'cache' => true,
'placeholder' => 'Select an example',
]);
}
}
// App\Security\EntityVoter.php
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity;
class EntityVoter extends Voter
{
const ENTITIES_BY_ROLE = [
'ROLE_USER' => [
Entity\Example::class
],
'ROLE_ADMIN' => [
Entity\User::class
]
];
protected function supports($access, $subject)
{
return $access === 'view' && gettype($subject) === 'string';
}
protected function voteOnAttribute($access, $subject, TokenInterface $token)
{
$roles = $token->getRoles();
foreach ($roles as $role) {
if (in_array($subject, self::ENTITIES_BY_ROLE[$role->getRole()])) {
return true;
}
}
return false;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.