Show only country_id =1 in list (Symfony + Sonata) - symfony

This is my simple listing of the "News" entity
protected function configureListFields(ListMapper $list): void
{
$list
->filte
->add('id')
->add('name')
->add('description')
->add('createdAt')
->add('updatedAt')
->add(ListMapper::NAME_ACTIONS, null, [
'actions' => [
'show' => [],
'edit' => [],
'delete' => []
],
]);
}
The News entity also has a country_id field (I won't show).
I need show only the news with country_id = 1.
How?

Ok, I solved with documentation
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$rootAlias = current($query->getRootAliases());
$query->andWhere(
$query->expr()->eq($rootAlias . '.my_field', ':my_param')
);
$query->setParameter('my_param', 'my_value');
return $query;
}
But need made several changes beacuse $query not is a QueryBuilder Object.
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$query_qb = $query->getQueryBuilder();
$rootAlias = current($query->getQueryBuilder()->getRootAliases());
$query_qb->andWhere(
$query_qb->expr()->eq($rootAlias . '.idCountry', ':idCountry')
);
$query_qb->setParameter('idCountry', $this->security->getUser()->getCountry()->getId());
return $query;
}

Related

orderby parameter is not added to query string

I have created a Wordpress plugin with an admin part displaying a table.
To accomplish this, I have created a class extending WP_List_Table all works perfectly but sorting.
When I see the header link, this is displayed:
https://example.com/wp-admin/admin.php?page=myplugin&order=asc
Notice that the orderby parameter is not present. Only the order parameter.
This the implementation of get_columns and get_sortable_columns method:
function get_columns() {
return $columns = array(
'col_alumno_id' => __('ID'),
'col_alumno_nombres' => __('Nombres'),
'col_alumno_apellidos' => __('Apellidos'),
'col_alumno_rut' => __('RUT'),
'col_alumno_curso' => __('Curso'),
'col_alumno_apoderados' => __('Apoderados')
);
}
public function get_sortable_columns() {
return $sortable = array(
'col_alumno_id' => 'ID',
'col_alumno_nombres' => 'alumno_nombres',
'col_alumno_apellidos' => 'alumno_apellidos',
'col_alumno_rut' => 'alumno_rut',
'col_alumno_curso' => 'curso_nombre'
);
}
Also, I have this at the end of prepare_items method:
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$_wp_column_headers[$screen->id] = $columns;
$this->_column_headers = array($columns, $hidden, $sortable);
/* -- Fetch the items -- */
$this->items = $wpdb->get_results($wpdb->prepare($query, $this->colegio));
What is missing here?

Symfony 5 - Display id and slug in category url and find it by id when slug is updated

My category url contains both id and slug like https://myapp.com/category/56-category-name (id field = 56 and slug field = category-name in the DB), when updating category name the slug field in DB is updated but the id still the same.
I would like to display my category whatever the slug provided that the id is correct and of course display the correct url. In this case SEO still correct i think.
Here my show method :
/**
* #Route("/category/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug, $id): Response
{
$category = $categoryRepository->find($id);
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
It works if the given id exists in DB, othewise i got an error Call to a member function getSlug() on null. I can throw a NotFoundException, but i think this method use many times queries.
So please help me doing these properly with more flexibility and performance.
In case I would like to display the category in the post url as well like https://myapp.com/56-category-name/23-post-title how to go about it ??
Thanks in advance
Use findOneBy() and check if the result is null
/**
* #Route("/category/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
* #Route("/{id}-{slug}/{idArticle}-{postSlug}", name="article_show", requirements={"idArticle"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug, $id, $idArticle = false, $postSlug = false ): Response
{
$category = $categoryRepository->findOneBy(['id'=>$id]);
if(is_null($category)){
throw new NotFoundHttpException(); //404, nothing found
}else{
//Category found.
if($idArticle){ // https://myapp.com/56-category-name/23-post-title
//Article route, put your logic here
}else{ //https://myapp.com/category/56-category-name
// Category route, logic here
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
}
}
here is what i found good for my app :
in my CategoryController.php i create, the show method :
/**
* #Route("/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug = null, $id): Response
{
$category = $categoryRepository->find($id);
if (!$category) {
throw new NotFoundHttpException();
}
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
in my index template to display the category_show link :
show
in my ArticleController.php i create, the show method :
/**
* #Route("/{category}/{id}-{slug}", name="article_show", requirements={"id"="\d+"})
*/
public function show(ArticleRepository $articleRepository, $slug = null, $id, $category = null): Response
{
$article = $articleRepository->find($id);
if (!$article) {
throw new NotFoundHttpException();
}
$cat = $article->getCategory()->getId() . '-' . $article->getCategory()->getSlug();
if($article->getSlug() !== $slug || $cat !== $category) {
return $this->redirectToRoute('article_show', [
'id' => $id,
'slug' => $article->getSlug(),
'category' => $cat
]);
}
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
in my index template to display the article_show link :
show
For me that solves my problem. But still, if we can improve the process, I'm a buyer.
Thanks

Add links on Wordpress custom endpoint

I created a custom endpoint for specific data from a custom table in my Wordpress plugin. It get's all the data from the table with the getHelpers() function. After that it will be merged by some user data. I would like to add the profile_image as a link to the response so we can get it with the embed parameter.
What is the best way to add the link to the response? I know the $response->add_link() function but this would add it to the response and not to each contributor.
I tried to add the link as an array but this won't react on the _embed parameter.
This is my code for the custom endpoint:
class VEMS_Rest_Contributors extends WP_REST_Controller {
protected $namespace = 'vems/v2';
protected $rest_base = 'contributors';
/**
* Register the routes for coupons.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'args' => $this->get_collection_params(),
) );
}
public function get_items( WP_REST_Request $request ) {
$project_id = $request->get_param( 'project_id' );
$contributors = array();
if( !empty($project_id) ) {
$project = new VEMS_Project( $request['project_id'] );
$helpers = $project->getHelpers();
foreach($helpers as $helper) {
$contributor = array();
if( !empty($helper->contributor_id) ) {
$user = get_user_by( 'ID', $helper->contributor_id );
$user_meta = get_user_meta( $helper->contributor_id );
$contributor['ID'] = $helper->contributor_id;
$contributor['user_nicename'] = $user->data->display_name;
$contributor['user_profile_image'] = $user_meta['contributor_profile_image'][0];
} else {
$contributor['user_nicename'] = $helper->name;
$contributor['user_profile_image'] = $helper->image_id;
}
$contributor['item_total'] = $helper->item_total;
$contributor['checked'] = $helper->checked;
$contributor['helper_date'] = $helper->helper_date;
/*
$contributor['_links']['profile_image'] = array(
'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ),
'embeddable' => true
);
*/
$contributors[] = $contributor;
}
}
$response = rest_ensure_response( $contributors );
return $response;
}
public function get_collection_params() {
$params['project_id'] = array(
'description' => __( 'Limit result set to contributors assigned a specific project.', 'vems' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
to handle links on route vems/v2/contributors?_embed, the element profile_image must be an array of links and then you can do that
$contributor['_links']['profile_image'] = [
[
'href' => rest_url( '/wp/v2/media/' . $contributor['ID'] ),
'embeddable' => true,
],
];

Sonata Admin list custom query for ListMapper sonata_type_model field

I try to get some entrys which match with all of my step objects but I don't have direct relation beetween them so I need to use a custom query.
I tried that in my admin class :
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('texteEtape', null, array('label' => $this->getTranslator()->trans('label.step.text')));
$this->addCountryListMap($listMapper);
$listMapper
->addIdentifier('id')
->add('temperature')
->add('rincage', null, array('label' => $this->getTranslator()->trans('label.rinsing')))
->add('concentration')
->add('temps', null, array('label' => $this->getTranslator()->trans('label.time')))
->add('produit', null, array('label' => $this->getTranslator()->trans('label.product')))
->add('enabled', null, array('editable' => true))
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array()
)
))
->add('updatedAt')
->add('Protocols','sonata_type_model', array('required' => true,
'class'=> 'HypredMainBundle:Protocole','property'=> 'name',
'query_builder' => $this->getProtocoles($listMapper->get('id'))));
The getProtocoles function :
private function getProtocoles($id) {
$querybuilder = $this->getManager()->getRepository('HypredMainBundle:Etape')->getStepProtocoles($id);
var_dump($querybuilder->getQuery()->getResult());
die();
return $querybuilder;
}
I would like to know how pass current entity id also (identifier return a FieldDescription object).
Maybe I miss something or something, I hope my post is comprehensive.
Thanks in advance for your time.
I think the way, that you try, wont work.
I would try to define a custom template for Protocols attribute
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('Protocols','string', array('template' => 'HypredMainBundle:Protocole:list_protocole.html.twig'))
;
}
Inside you will have object and its id
{{ object.id | protocols() }}
Than I would wrote a Twig Extension
class AppExtension extends \Twig_Extension
{
protected $container;
public function __constructor($container)
{
// remember to pass #service_container defining a twig extension service
$this->container = $container;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('protocols', array($this, 'protocolsFilter')),
);
}
public function ProtocolsFilter($id)
{
// content of getProtocoles() function from question
$querybuilder = $this->container->get('doctrine')->getManager()->getRepository('HypredMainBundle:Etape')->getStepProtocoles($id);
var_dump($querybuilder->getQuery()->getResult());
die();
return $querybuilder;
}
public function getName()
{
return 'app_extension';
}
}

How to add a custom callback method in configureListFields() sonata admin?

I want to add a custom callback method on a sonata field list configureListFields()
It's possible in the configureDatagridFilters() method like below:
protected function configureDatagridFilters(DatagridMapper $filter)
{
$filter
->add('user', 'doctrine_orm_callback', array(
'callback' => array($this, 'customMethod'),
));
}
But is it possible in the configureListFields() method ?
Your attempt using the doctrine_orm_callback is correct:
Doctrine2 ORM Admin's documentation - Reference - Filter Field Definition (2.2) - 5.5.3. Callback
Use the provided example and replace comments with your bookings and then apply your conditions to the builder:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('enabled')
->add('tags', null, array(), null, array('expanded' => true, 'multiple' => true))
->add('author')
->add('with_open_comments', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!$value) {
return;
}
$queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c');
$queryBuilder->andWhere('c.status = :status');
$queryBuilder->setParameter('status', Comment::STATUS_MODERATE);
return true;
},
'field_type' => 'checkbox'
))
;
}

Resources