class ConfigureActivityForm in Activity 8
Configure activities form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\activity\Form\MultiStepFormBase
- class \Drupal\activity\Form\ConfigureActivityForm
- class \Drupal\activity\Form\MultiStepFormBase
Expanded class hierarchy of ConfigureActivityForm
1 string reference to 'ConfigureActivityForm'
File
- src/
Form/ ConfigureActivityForm.php, line 22
Namespace
Drupal\activity\FormView source
class ConfigureActivityForm extends MultiStepFormBase {
/**
* The tree builder.
*
* @var \Drupal\token\TreeBuilderInterface
*/
protected $treeBuilder;
/**
* The token entity mapper.
*
* @var \Drupal\token\TokenEntityMapperInterface
*/
protected $entityMapper;
/**
* The database.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Activity service.
*
* @var \Drupal\activity\QueryActivity
*/
protected $activityService;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The event name.
*
* @var string
*/
protected $label;
/**
* The event when the action should happen.
*
* @var string
*/
protected $hook;
/**
* The event id.
*
* @var string
*/
protected $eventId;
/**
* ConfigureActivityForm constructor.
*
* @param \Drupal\token\TreeBuilderInterface $tree_builder
* The tree builder.
* @param \Drupal\token\TokenEntityMapperInterface $entity_mapper
* The entity mapper.
* @param \Drupal\Core\Database\Connection $database
* The database.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Drupal\Core\Session\SessionManagerInterface $sessionManager
* The session manager.
* @param \Drupal\user\PrivateTempStoreFactory $tempStoreFactory
* The temp store factory.
* @param \Drupal\activity\QueryActivity $activityService
* The activity service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(TreeBuilderInterface $tree_builder, TokenEntityMapperInterface $entity_mapper, Connection $database, AccountInterface $currentUser, SessionManagerInterface $sessionManager, PrivateTempStoreFactory $tempStoreFactory, QueryActivity $activityService, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler) {
parent::__construct($currentUser, $sessionManager, $tempStoreFactory);
$this->treeBuilder = $tree_builder;
$this->entityMapper = $entity_mapper;
$this->database = $database;
$this->activityService = $activityService;
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->label = $this->store
->get('label');
$this->hook = $this->store
->get('hook');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('token.tree_builder'), $container
->get('token.entity_mapper'), $container
->get('database'), $container
->get('current_user'), $container
->get('session_manager'), $container
->get('user.private_tempstore'), $container
->get('query_activity'), $container
->get('entity_type.manager'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'configure_activities_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Default value for Activity window field.
$windowDefault = '';
// Default value for roles field.
$rolesDefault = '';
// Default value for content types field.
$typesDefault = '';
// Default value for message field.
$messageDefault = '';
$form = parent::buildForm($form, $form_state);
$current_path = \Drupal::service('path.current')
->getPath();
$result = \Drupal::service('path.alias_manager')
->getAliasByPath($current_path);
$path_args = explode('/', $result);
// The event id.
$this->eventId = $path_args[4];
// If event exists, get label form database.
if ($this->eventId != 'new') {
$query = $this->activityService
->getActivityEventField($this->eventId, 'label');
$this->label = $query[0]->label;
$query = $this->activityService
->getActivityEventField($this->eventId, 'hook');
$this->hook = $query[0]->hook;
}
// Get fields values if they exist.
$messageJson = $this->activityService
->getActivityEventField($this->eventId, 'message');
if (!empty($messageJson)) {
$activityMessage = json_decode($messageJson[0]->message);
$windowDefault = $activityMessage->window;
$rolesDefault = $activityMessage->roles;
$typesDefault = $activityMessage->types;
$messageDefault = $activityMessage->message;
}
// Event name.
$form['activity_label'] = [
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $this->label,
'#required' => TRUE,
'#size' => 30,
'#attributes' => [
'class' => [
'activity_label',
],
],
];
// Options for activity window.
$timeIntervals = range(0, 7200, 300);
$options = $this
->buildOptions($timeIntervals);
$options[0] = 'Unlimited';
$form['activity_window'] = [
'#type' => 'select',
'#title' => t('Activity Window'),
'#description' => t('Prevent repeat Activity from the same user for the same entity within this interval'),
'#options' => $options,
'#default_value' => $windowDefault == '' ? $this->options['window'] : $windowDefault,
];
// Add roles or content types options based on hook.
// Do not need content types when need to update user for example.
if (strpos($this->hook, 'user') !== FALSE) {
$roles = Role::loadMultiple();
$roleOptions = [];
foreach ($roles as $role => $value) {
$roleOptions[$role] = $role;
}
$form['activity_roles'] = [
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#default_value' => $rolesDefault,
'#options' => $roleOptions,
];
}
else {
$bundles = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$contentTypes = array_keys($bundles);
$types = array_combine($contentTypes, $contentTypes);
$form['activity_node_types'] = [
'#type' => 'checkboxes',
'#title' => t('Allowed Node Types'),
'#default_value' => $typesDefault,
'#options' => $types,
];
}
// Message that keeps all the options.
$form['activity_message'] = [
'#type' => 'textarea',
'#title' => t('Public Message'),
'#description' => t('Message displayed to everyone who is not part of this Activity.'),
'#default_value' => $messageDefault,
];
// The token.module provides the UI for the tokens when module enabled.
$moduleHandler = $this->moduleHandler;
if ($moduleHandler
->moduleExists('token')) {
// Get tokens options.
$token_tree = $this->treeBuilder
->buildAllRenderable([
'click_insert' => TRUE,
'show_restricted' => TRUE,
'show_nested' => FALSE,
]);
// Interest only on these types.
$tokenFor = [
'current-date' => 'current-date',
'current-page' => 'current-page',
'current-user' => 'current-user',
'node' => 'node',
'user' => 'user',
'random' => 'random',
'site' => 'site',
];
if (strpos($this->hook, 'user') !== FALSE) {
unset($tokenFor['node']);
}
elseif (strpos($this->hook, 'comment') !== FALSE) {
$tokenFor['comment'] = 'comment';
}
foreach ($token_tree['#token_tree'] as $key => $value) {
if (!in_array($key, $tokenFor)) {
unset($token_tree['#token_tree'][$key]);
}
}
$form['token_help'] = [
'#type' => 'markup',
'#markup' => \Drupal::service('renderer')
->render($token_tree),
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
];
return $form;
}
/**
* Window interval.
*/
public function buildOptions(array $timeIntervals) {
$callback = [
$this,
'callbackOptions',
];
return array_combine($timeIntervals, array_map($callback, $timeIntervals));
}
/**
* Callback for buildOptions function.
*/
public function callbackOptions($value, $langcode, $granularity = 2) {
return \Drupal::service('date.formatter')
->formatInterval($value, $granularity, $langcode);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// If the event is not new, get hook from database.
if ($this->hook == NULL) {
$query = $this->activityService
->getActivityEventField($this->eventId, 'hook');
$this->hook = $query[0]->hook;
}
// Get options to insert them in database.
$userId = \Drupal::currentUser()
->id();
$this->label = $form_state
->getValue('activity_label');
$windowOption = $form_state
->getValue('activity_window');
$roleOptions = $form_state
->getValue('activity_roles') == NULL ? NULL : array_filter(array_values($form_state
->getValue('activity_roles')));
$contentTypes = $form_state
->getValue('activity_node_types') == NULL ? NULL : array_filter(array_values($form_state
->getValue('activity_node_types')));
$message = $form_state
->getValue('activity_message');
$messageArray = [
'window' => $windowOption,
'roles' => $roleOptions,
'types' => $contentTypes,
'message' => $message,
];
// Update row based on event id.
if ($this->eventId != 'new') {
$this->database
->update('activity_events')
->fields([
'label' => $this->label,
'hook' => $this->hook,
'userId' => $userId,
'message' => json_encode($messageArray),
])
->condition('event_id', $this->eventId)
->execute();
}
else {
$this->database
->insert('activity_events')
->fields([
'label' => $this->label,
'hook' => $this->hook,
'userId' => $userId,
'created' => \Drupal::time()
->getCurrentTime(),
'message' => json_encode($messageArray),
])
->execute();
}
// Set label and hook to be null.
$this
->deleteStore();
$url = Url::fromUri('internal:/admin/activity');
$form_state
->setRedirectUrl($url);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigureActivityForm:: |
protected | property | Activity service. | |
ConfigureActivityForm:: |
protected | property | The database. | |
ConfigureActivityForm:: |
protected | property | The token entity mapper. | |
ConfigureActivityForm:: |
protected | property | The entity type manager. | |
ConfigureActivityForm:: |
protected | property | The event id. | |
ConfigureActivityForm:: |
protected | property | The event when the action should happen. | |
ConfigureActivityForm:: |
protected | property | The event name. | |
ConfigureActivityForm:: |
protected | property | The module handler. | |
ConfigureActivityForm:: |
protected | property | The tree builder. | |
ConfigureActivityForm:: |
public | function |
Form constructor. Overrides MultiStepFormBase:: |
|
ConfigureActivityForm:: |
public | function | Window interval. | |
ConfigureActivityForm:: |
public | function | Callback for buildOptions function. | |
ConfigureActivityForm:: |
public static | function |
Instantiates a new instance of this class. Overrides MultiStepFormBase:: |
|
ConfigureActivityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConfigureActivityForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ConfigureActivityForm:: |
public | function |
ConfigureActivityForm constructor. Overrides MultiStepFormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MultiStepFormBase:: |
private | property | The current user. | |
MultiStepFormBase:: |
private | property | The session manager. | |
MultiStepFormBase:: |
protected | property | The store. | |
MultiStepFormBase:: |
protected | property | The tempstore factory. | |
MultiStepFormBase:: |
protected | function | Set to null label and hook used for the multistep form. | |
MultiStepFormBase:: |
protected | function | Saves the data from the multistep form. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |