You are here

class ConfigureActivityForm in Activity 8

Configure activities form.

Hierarchy

Expanded class hierarchy of ConfigureActivityForm

1 string reference to 'ConfigureActivityForm'
activity.routing.yml in ./activity.routing.yml
activity.routing.yml

File

src/Form/ConfigureActivityForm.php, line 22

Namespace

Drupal\activity\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfigureActivityForm::$activityService protected property Activity service.
ConfigureActivityForm::$database protected property The database.
ConfigureActivityForm::$entityMapper protected property The token entity mapper.
ConfigureActivityForm::$entityTypeManager protected property The entity type manager.
ConfigureActivityForm::$eventId protected property The event id.
ConfigureActivityForm::$hook protected property The event when the action should happen.
ConfigureActivityForm::$label protected property The event name.
ConfigureActivityForm::$moduleHandler protected property The module handler.
ConfigureActivityForm::$treeBuilder protected property The tree builder.
ConfigureActivityForm::buildForm public function Form constructor. Overrides MultiStepFormBase::buildForm
ConfigureActivityForm::buildOptions public function Window interval.
ConfigureActivityForm::callbackOptions public function Callback for buildOptions function.
ConfigureActivityForm::create public static function Instantiates a new instance of this class. Overrides MultiStepFormBase::create
ConfigureActivityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConfigureActivityForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ConfigureActivityForm::__construct public function ConfigureActivityForm constructor. Overrides MultiStepFormBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MultiStepFormBase::$currentUser private property The current user.
MultiStepFormBase::$sessionManager private property The session manager.
MultiStepFormBase::$store protected property The store.
MultiStepFormBase::$tempStoreFactory protected property The tempstore factory.
MultiStepFormBase::deleteStore protected function Set to null label and hook used for the multistep form.
MultiStepFormBase::saveData protected function Saves the data from the multistep form.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.