Custom form field in views - drupal-8

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++;
}
}

Related

Livewire Lifecycle Hooks self update to upper

Hi is it possible to make live self update to upper form field payment?
because that works if set another field of form not the same.
public function updatingpayment($value)
{
$this->upper($value);
}
public function upper($value){
$process = strtoupper($value);
$this->payment=$process;
}
You still need to reference the field from the lifecycle method, Livewire doesn't know your intention.
public function updatedPayment($value)
{
$this->payment = strtoupper($value);
}

How to catch a form when it's submitted and forward form values to Dotmailer?

I've been asked to make a change to a Drupal 8 site (I'm not a Drupal Developer), the client would like an enquiry form linking up with Dotmailer. Within the theme I've tried to create a handler based on some information that I found online. But I don't know if it's doing anything. I've checked the logs section of Drupal and there isn't anything logged to indicate that the custom handler was actioned.
My theme is named abc-primary, and inside the theme folder I have created the file abc_primary.theme with the following contents;
<?php
use Drupal\media\Entity\Media;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_system_theme_settings_alter().
*/
function abc_primary_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$theme_file = drupal_get_path('theme', 'abc_primary') . '/abc_primary.theme';
$build_info = $form_state->getBuildInfo();
if (!in_array($theme_file, $build_info['files'])) {
$build_info['files'][] = $theme_file;
}
$form_state->setBuildInfo($build_info);
$form['#submit'][] = 'abc_primary_form_system_theme_settings_submit';
}
function abc_primary_form_system_theme_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// TODO: Extra submission logic.
// This submit handler will be called before default submit handler for this form.
\Drupal::logger('mymodule')->notice('mymodule submit ') ;
}
Is the above correct or am I doing something wrong?
Create a new module, and define following method:
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
//you may want to check your form_id by
// $form['#form_id']
$form['actions']['submit']['#submit'][] = 'mymodule_node_form_submit';
}
function mymodule_node_form_submit($form, FormStateInterface $form_state) {
// get values from form_state
//log or send to desired web service
}
Hope it helps.

Drupal 8 subscribe to an event from inside block

I am currently trying to wrap my head around Drupal 8 module development best practices. All I'm trying to do is to have a simple form Demoform on a page where a user can input an email address. When the form gets submitted I'd like to dispatch an event demo_form.save. Also I need a block that then displays the user's email address within the block (let's say sidebar second). I have already implemented an EventSubscriber before as a test, so the event gets properly dispatched etc. and I also subscribed to the event (but how to get the information inside a block) Now my question: what's the best practice for this workflow:
File DemoForm.php
class DemoForm extends ConfigFormBase {
...
$event = $dispatcher->dispatch('demo_form.save', $e);
...
}
File DemoEventSubscriber.php
class DemoEventSubscriber implements EventSubscriberInterface {
static function getSubscribedEvents() {
$events['demo_form.save'][] = array('onConfigSave', 0);
return $events;
}
public function onConfigSave($event) {
...
}
}
This works and I can access the input from the form inside the DemoEventSubscriber class and do whatever I want with it.
But now I'd like to display the email address inside the block markup. How should this best be done ?
File DemoBlock.php
class DemoBlock extends BlockBase {
public function build() {
// here return markup with email address from form
}
}
How do I combine the eventsubscriber and the block markup ? Can Blockbase itself implement the EventSubscriberInterface and be independent from DemoEventSubscriber.php ? Or do I need to register a service that transmits the form data and then access the service within the block's build() function ? Or is there another way I am missing ?
Thanks for any input.
I am not sure what you need the event for, but to dispatch the event, use the code you have already displayed in your submitForm() function of the DemoForm class.
Because you are using ConfigFormBase, I assume that you want to store the submitted e-mail address in config, use code like from the config form documentation:
/**
* {#inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Retrieve the configuration
$this->config('mymodule.settings')
// Set the submitted configuration setting
->set('email', $form_state->getValue('email'))
->save();
// Assuming you have injected the dispatcher.
$event = $this->dispatcher->dispatch('demo_form.save', $e);
parent::submitForm($form, $form_state);
}
Within you block, you can then access the configuration for example with the static wrapper or inject the service Simple Configuration API
$config = \Drupal::config('mymodule.settings');
$message = $config->get('email');
Note that with this you can always set only one e-mail address. I don't know if that was your purpose. If you want to collect multiple e-mails then you should store them in the database and not in config.

How to add multiple forms Joomla 2.5 component

I am developing a joomla website which required some custom component to integrate manage portfolio, user profile etc. We downloaded one of the hello_world MVC component for joomla 2.5[http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Introduction] and did necessary customization. First component worked well. But now we need a new component that is having multiple forms required to integrate.
Eg: Store types for one form, store details for another form, manage country/ state by another form.
In the default component having option for manage one form [add/edit/delete/view]. Here I tried to modify/replicate but I failed.
Can anyone suggest the architecture/ sample code to manage multiple forms [Add/edit/delete/view] in joomla 2.5 component creation.
Any help will be apreciate?
Supposing You're speaking of forms stored in the model/forms folder... You should try to override the getForm() function in your model, to call the right form. You should pass a 'layout' when calling the page and then get it in the model constructor.
May be so:
class YourComponentModelYourModel extends JModelAdmin{
public function __construct($config = array()){
switch(JRequest::getVar('layout')){
case 'firstlayout' : $this->form='firstform';
break;
case 'secondlayout' : $this->form='secondform';
break;
default : $this->form='defaultform';
}
parent::__construct($config);
}
...
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_yourcomponent.'.$this->form,$this->form,
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)){return false;}
return $form;
}
You must put a layout for each form in the views/YourView/tmpl folder and the form declaration must call the layout also :
<form action="<?php echo JRoute::_('index.php?option=com_yourcomponent&layout=firstlayout&id='.(int) $this->item->id); ?>"
method="post" name="adminForm" id="draw-form">

Adding a search box to filter a list of results in Symfony?

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.