Commit 1f581346 authored by ErikHommel's avatar ErikHommel

completed first version of documentation

parent e6dba072
......@@ -118,7 +118,7 @@ The first step is create a class in my extension extending the *AbstractAction*
````php
<?php
namespace Civ**i\Myemailprefs\Actions;
namespace Civi\Myemailprefs\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
......@@ -183,7 +183,7 @@ The method *doAction* actually performs my action, so in this case use the conta
* @throws InvalidParameterException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = (int) $parameters->getParameter(['contact_id']);
$contactId = (int) $parameters->getParameter('contact_id');
if ($contactId) {
$contactGroups = $this->retrieveContactGroups($contactId);
$outputValues = $this->calculateOutputValues($contactGroups);
......@@ -203,13 +203,135 @@ This is all that is needed for my action to get the email preferences.
## Develop my action to save the new email preferences
In this action I will receive the output of the form and process that into the desired group memberships.
I expect a contact_id parameter, and the tickboxes from the form.
The first step is create a class in my extension extending the *AbstractAction* class from the **Action Provider**. This will initially have to look like this:
````php
namespace Civi\Myemailprefs\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\InvalidParameterException;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use Civi\FormProcessor\API\Exception;
use CRM_Myemailprefs_ExtensionUtil as E;
/**
* Class SetEmailPrefs - set the email preferences of a contact from form data
*/
class SetEmailPrefs extends AbstractAction {
````
Next step is to specify the required methods *getParameterSpecification*, *getConfigurationSpecification* and *doAction*.
The method *getParameterSpecification* determines the parameters I expect to receive in my class. This is the data that will be sent to my action from my form processor or from one of the previous actions.
In this example I expect to get a contact ID and all the tick boxes as parameters:
````php
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
$specs->addSpecification(new Specification('newsletter_and_action', 'Boolean', E::ts('Monthly Newsletter and Monthly Action'), FALSE, FALSE));
$specs->addSpecification(new Specification('newsletter_only', 'Boolean', E::ts('Monthly Newsletter Only'), FALSE, FALSE));
$specs->addSpecification(new Specification('yearly_summary', 'Boolean', E::ts('Yearly Summary Only'), FALSE, FALSE));
$specs->addSpecification(new Specification('no_general_mail', 'Boolean', E::ts('No General Mails'), FALSE, FALSE));
return $specs;
}
````
The method *getConfigurationSpecification* is not really needed here because I have no configuration to set. But I do need this function in my class (as specified in the abstract class) so I make it 'void':
````php
/**
* @return SpecificationBag|void
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
````
The *doAction* method performs my actual actions, so will translate the tick boxes into group memberships and add/remove the contact from the relevant groups.
````php
/**
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws InvalidParameterException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = (int) $parameters->getParameter('contact_id');
if ($contactId) {
$this->setGroupMemberships($contactId, $parameters);
}
else {
throw new InvalidParameterException(E::ts("Could not find mandatory parameter contact_id"));
}
}
````
This is all that is needed for my action to set the email preferences.
!!! Note "I want the rest too"
If you want to also see what the method *setGroupMemberships* does, check the code on [My Email Prefs Code on Gitlab][myemailprefs]
## Add my actions to the Action Provider
## Set up the form processor
As a last step I need to tell CiviCRM about my actions.
First I need to add a CompilerPass Class to my extension:
````php
namespace Civi\Myemailprefs;
/**
* Compiler Class for action provider
*/
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use CRM_Myemailprefs_ExtensionUtil as E;
class CompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
if ($container->hasDefinition('action_provider')) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction',
['GetEmailPrefs', 'Civi\Myemailprefs\Actions\GetEmailPrefs', E::ts('Get Email Preferences'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['SetEmailPrefs', 'Civi\Myemailprefs\Actions\SetEmailPrefs', E::ts('Set Email Preferences'), []]);
}
}
}
````
And then I need to add my CompilerPass Class to the CiviCRM container using the container hook in my main extension file *myemailprefs.php*:
````php
/**
* Implements hook_civicrm_container().
*
* @param ContainerBuilder $container
*/
function myemailprefs_civicrm_container(ContainerBuilder $container) {
$container->addCompilerPass(new Civi\Myemailprefs\CompilerPass());
}
````
## Form
## Set up the form
I have completed the code for my actions, tested them with the API Explorer with entities *FormProcessor* and *FormProcesserDefaults*.
## Result!
Now I could set up a form on my website to work with the actions. This is outside the scope of this section, the ambition here was to explain how to create your own actions.
If you want to see an example of how to set up a form check [Newsletter Drupal7 Example](sign-up-newsletter.md) or [Newsletter Wordpress Example](sign-up-newsletter-wordpress.md).
[dataprocessor]:https://civicrm.org/extensions/data-processor
[aghchecksum]:https://aghstrategies.com/content/how-create-one-click-personalized-links-civicrm-emails
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment