For a custom Views Views Bulk Operations action, I would like to enhance the information in the list on the confirm page, For example, instead of:
LastName1
LastName2
I would like to have:
LastName1, FirstName, Prefix
LastName2, FirstName, Prefix
Where is the best place to alter this?
There are basically two ways to do this in Drupal 8:
using hook_form_views_bulk_operations_confirm_action_alter. This will allow you to alter the list, for example with custom values and code.
If you have defined a custom action plugin, then in the plugin annotiation you can declare the route name to a custom validation form. This will allow you to do anything you want, including multi-step forms. When you define that custom form, you should subclass Drupal\views_bulk_operations\Form\ConfirmAction, since it's buildForm method takes parameters additional to that of a regular form. So your Plugin would start like this:
/**
* Action description.
*
* #Action(
* id = "my_special_action",
* label = #Translation("My Special Action"),
* type = "custom_entity",
* confirm_form_route_name = "my_module.my_special_action_confirm_form",
* )
*/
class MySpecialAction extends ViewsBulkOperationsActionBase {
}
and the Form will look something like this:
use Drupal\views_bulk_operations\Form\ConfirmAction;
class MySpecialActionConfirmForm extends ConfirmAction {
public function getFormId() {
return 'my_special_action_confirm_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $view_id = NULL, $display_id = NULL) {
....
}
In the custom form class, you will have to define your own submitForm method, if you want to pass anything special to the custom action.
Related
I'm trying to create a custom field for a view in Drupal 8 which allows to perform an action without changing the page (link). I guess I have to create a form inside that custom field but I do not know how to achieve it.
Any idea on how to do it or other alternative without redirecting to a route?
The view will be a list of custom entities and I need a button for each of the lines.
Thanks in advance!
Finally I solved it by following this steps:
I created a view custom field (generate:plugin:views:field with
drupal console)
I created a form (generate:form)
Then, in the view custom field render function return the form:
$form = \Drupal::formBuilder()->getForm('Drupal\test_module\Form\TestForm', $values->_entity->ID());
return $form;
It's important to notice that an incremental (dynamic) formId is needed for things to work properly. I did that by creating a static variable and a __construct() method as follows:
protected static $instanceId;
public function getFormId() {
return 'my_form_id_' . self::$instanceId;
}
public function __construct(){
if (empty(self::$instanceId)) {
self::$instanceId = 1;
}
else {
self::$instanceId++;
}
}
I have a custom view that creates a tab on a node page. I have several content types, but I only want the tab to show on some of them. If this were a regular route, I'd just throw a custom_access under requirements, but there doesn't seem to be a way to do that with routes created outside a routing.yml file.
Is there a reasonable way to do this?
You need to create custom route subscriber. File custom_module.services.yml:
services:
custom_module.route_subscriber:
class: Drupal\custom_module\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
File RouteSubscriber.php:
<?php
namespace Drupal\custom_module\Routing;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {#inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if($route = $collection->get('view.<view_name>.<view_bundle>')){ // Need to change view_name and view_bundle.
$route->setRequirement(
'_custom_access',
'\Drupal\custom_module\Routing\RouteSubscriber::viewsAccess'
);
}
}
public function viewsAccess() {
return AccessResult::allowedIf(
// Add condition when view has access
);
}
}
I want validate an entity doctrine differently when the entity is created, updated or deleted.
There is an entity constraint validator in my entity class.
// src/AppBundle/Entity/AcmeEntity.php
use AppBundle\Validator\Constraints as AcmeAssert;
/**
* #AcmeAssert\CustomConstraint
*/
class AcmeEntity
{
// ...
protected $name;
// ...
}
In my CustomConstraint I want determine if the Entity will be updated, created or delete for execute a specific validator.
Using unit of work is a solution ?
What is the best way to make this?
I think this problematic is common in lot of application ?
Thank's all ;)
You could either use validation groups based on the submitted data or handle itwhen you create the form by passing the validation group.
For example, in your controller when you create the form;
$form = $this->createForm(new AcmeType(), $acme, ['validation_groups' => ['create']]);
Then you entity would be something like;
/**
* Get name
*
* #Assert\Length(min=2, max=11, groups={"create", "update"})
* #AcmeAssert\ContainsAlphanumeric(groups={"create"}) // only applied when create group is passed
* #return string
*/
public function getName()
{
return $this->name;
}
This is what validation groups are made for.
Since Symfony Forms read validations from entity annotations and use internally the Validator component you'd have a look at these articles in the documentation:
http://symfony.com/doc/current/form/validation_groups.html
http://symfony.com/doc/current/validation/groups.html
http://symfony.com/doc/current/validation/sequence_provider.html
I've tried to add a dynamic validator to a collection but everything I've tried hasn't worked.
If I have - let's say - 3 fieldsets in a collection, I only want the second fieldset (with name "1") to have a required field.
$input_filter = new Filter\Edit();
$collection_input_filter = new CollectionInputFilter();
$orderDetailFilter = new Filter\OrderDetail($serviceManager);
$collection_input_filter->setInputFilter($orderDetailFilter);
$input_filter->add($collection_input_filter, 'details');
$this->setInputFilter($input_filter)
->setHydrator(new DoctrineHydrator($entityManager))
->setObject(new Order());
Check this:
http://www.aronkerr.com/2013/11/zf2-form-collection-validation-unique.html
He adding unique values for an element in a fieldset collection. He extend the InputCollectionFilter.
You can do the same way for your custom key validation.
You have to extend the isValid Method from the CollectionInputFilter.
Write your own CollectionInputFilter and extend it from the Zend\InputFilter\CollectionInputFilter.
Add your own property:
protected validateFieldsetKeys = array()
for Example
with a setter Method.
When you´re adding your CollectionInputfilter (in your factory or controller), just call your own and add your fieldsetkeys like this:
$collection_input_filter = new CollectionInputFilter();
$collection_input_filter->setValidateFieldsetKeys(array(2,3));
I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.
I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).
I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.
I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?
EDIT
I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.
Thanks!
I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:
//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
//set the form filter
$this->searchForm = new EmployeeFormFilter();
//bind it empty to fetch all data
$this->searchForm->bind(array());
//fetch all
$this->employees = $this->searchForm->getQuery()->execute();
...
}
I made a search action which does the search
public function executeSearch(sfWebRequest $request)
{
//create filter
$this->searchForm = new EmployeeFormFilter();
//bind parameter
$fields = $request->getParameter($this->searchForm->getName());
//bind
$this->searchForm->bind($fields);
//set paginator
$this->employees = $this->searchForm->getQuery()->execute();
...
//template
$this->setTemplate("index");
}
It's important that the search form goes to mymodule/search action.
Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.
If you want to add more fields to the search form check this :)
I finally made a custom form using the default MyModuleForm generated by Symfony
public function executeIndex {
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
but displaying only a custom field:
<div id="search_box">
<input type="text" name="criteria" id="search_box_criteria" value="Search..." />
<?php echo link_to('Search', '#my_module_search?criteria=') ?>
</div>
Then I created a route named #my_module_search linked to the index action:
my_module_search:
url: my_module/search/:criteria
param: { module: my_module, action: index }
requirements: { criteria: .* } # Terms are optional, show all by default
With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:
$('#search_box a').click(function(){
$(this).attr('href', $(this).attr('href') + $(this).prev().val());
});
And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:
public function executeIndex {
...
// Deal with search criteria
if ( $text = $request->getParameter('criteria') ) {
$query = $this->pager->getQuery()
->where("MyTable.name LIKE ?", "%$text%")
->orWhere("MyTable.remarks LIKE ?", "%$text%")
...;
}
$this->pager->setQuery($query);
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.