You are here

class SupportTicketForm in Support Ticketing System 8

Form controller for the support ticket edit forms.

Hierarchy

Expanded class hierarchy of SupportTicketForm

File

modules/support_ticket/src/SupportTicketForm.php, line 20
Contains \Drupal\support_ticket\SupportTicketForm.

Namespace

Drupal\support_ticket
View source
class SupportTicketForm extends ContentEntityForm {

  /**
   * The tempstore factory.
   *
   * @var \Drupal\user\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Whether this support ticket has been previewed or not.
   */
  protected $hasBeenPreviewed = FALSE;

  /**
   * Constructs a ContentEntityForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   */
  public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory) {
    parent::__construct($entity_manager);
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.manager'), $container
      ->get('user.private_tempstore'));
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareEntity() {

    /** @var \Drupal\support_ticket\SupportTicketInterface $support_ticket */
    $support_ticket = $this->entity;
    if (!$support_ticket
      ->isNew()) {

      // Remove the revision log message from the original support ticket entity.
      $support_ticket->revision_log = NULL;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    // Try to restore from temp store, this must be done before calling
    // parent::form().
    $uuid = $this->entity
      ->uuid();
    $store = $this->tempStoreFactory
      ->get('support_ticket_preview');

    // If the user is creating a new support_ticket, the UUID is passed in the request.
    if ($request_uuid = \Drupal::request()->query
      ->get('uuid')) {
      $uuid = $request_uuid;
    }
    if ($preview = $store
      ->get($uuid)) {

      /** @var $preview \Drupal\Core\Form\FormStateInterface */
      $form_state = $preview;

      // Rebuild the form.
      $form_state
        ->setRebuild();
      $this->entity = $preview
        ->getFormObject()
        ->getEntity();
      unset($this->entity->in_preview);

      // Remove the stale temp store entry for existing support tickets.
      if (!$this->entity
        ->isNew()) {
        $store
          ->delete($uuid);
      }
      $this->hasBeenPreviewed = TRUE;
    }

    /** @var \Drupal\support_ticket\SupportTicketInterface $support_ticket */
    $support_ticket = $this->entity;
    if ($this->operation == 'edit') {
      $form['#title'] = $this
        ->t('<em>Edit @support_ticket_type</em> @title', array(
        '@support_ticket_type' => support_ticket_get_type_label($support_ticket),
        '@title' => $support_ticket
          ->label(),
      ));
    }
    $current_user = $this
      ->currentUser();

    // Changed must be sent to the client, for later overwrite error checking.
    $form['changed'] = array(
      '#type' => 'hidden',
      '#default_value' => $support_ticket
        ->getChangedTime(),
    );
    $form['advanced'] = array(
      '#type' => 'vertical_tabs',
      '#attributes' => array(
        'class' => array(
          'entity-meta',
        ),
      ),
      '#weight' => 99,
    );
    $form = parent::form($form, $form_state);

    // Add a revision_log field if the "Create new revision" option is checked,
    // or if the current user has the ability to check that option.
    $form['revision_information'] = array(
      '#type' => 'details',
      '#group' => 'advanced',
      '#title' => t('Revision information'),
      // Open by default when "Create new revision" is checked.
      '#open' => $support_ticket
        ->isNewRevision(),
      '#attributes' => array(
        'class' => array(
          'support-ticket-form-revision-information',
        ),
      ),
      '#attached' => array(
        'library' => array(
          'support_ticket/drupal.support_ticket',
        ),
      ),
      '#weight' => 20,
      '#optional' => TRUE,
    );
    $form['revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $support_ticket->support_ticket_type->entity
        ->isNewRevision(),
      '#access' => $current_user
        ->hasPermission('administer support tickets'),
      '#group' => 'revision_information',
    );
    $form['revision_log'] += array(
      '#states' => array(
        'visible' => array(
          ':input[name="revision"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#group' => 'revision_information',
    );

    // Support ticket author information for administrators.
    $form['author'] = array(
      '#type' => 'details',
      '#title' => t('Authoring information'),
      '#group' => 'advanced',
      '#attributes' => array(
        'class' => array(
          'support-ticket-form-author',
        ),
      ),
      '#attached' => array(
        'library' => array(
          'support_ticket/drupal.support_ticket',
        ),
      ),
      '#weight' => 90,
      '#optional' => TRUE,
    );
    if (isset($form['uid'])) {
      $form['uid']['#group'] = 'author';
    }
    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }

    // Support ticket options for administrators.
    $form['options'] = array(
      '#type' => 'details',
      '#title' => t('Locking options'),
      '#group' => 'advanced',
      '#attributes' => array(
        'class' => array(
          'support-ticket-form-options',
        ),
      ),
      '#attached' => array(
        'library' => array(
          'support_ticket/drupal.support_ticket',
        ),
      ),
      '#weight' => 95,
      '#optional' => TRUE,
    );
    if (isset($form['locked'])) {
      $form['locked']['#group'] = 'options';
    }
    $form['#attached']['library'][] = 'support_ticket/form';
    $form['#entity_builders']['update_status'] = [
      $this,
      'updateStatus',
    ];
    return $form;
  }

  /**
   * Entity builder updating the support_ticket status with the submitted value.
   *
   * @param string $entity_type_id
   *   The entity type identifier.
   * @param \Drupal\support_ticket\SupportTicketInterface $support_ticket
   *   The support_ticket updated with the submitted values.
   * @param array $form
   *   The complete form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @see \Drupal\support_ticket\SupportTicketForm::form()
   */
  function updateStatus($entity_type_id, SupportTicketInterface $support_ticket, array $form, FormStateInterface $form_state) {
    $element = $form_state
      ->getTriggeringElement();
    if (isset($element['#published_status'])) {
      $support_ticket
        ->setPublished($element['#published_status']);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $element = parent::actions($form, $form_state);
    $support_ticket = $this->entity;
    $preview_mode = $support_ticket->support_ticket_type->entity
      ->getPreviewMode();
    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $this->hasBeenPreviewed;

    // If saving is an option, privileged users get dedicated form submit
    // buttons to adjust the publishing status while saving in one go.
    // @todo This adjustment makes it close to impossible for contributed
    //   modules to integrate with "the Save operation" of this form. Modules
    //   need a way to plug themselves into 1) the ::submit() step, and
    //   2) the ::save() step, both decoupled from the pressed form button.
    if ($element['submit']['#access'] && \Drupal::currentUser()
      ->hasPermission('administer support tickets')) {

      // isNew | prev status » default   & publish label             & unpublish label
      // 1     | 1           » publish   & Save and publish          & Save as unpublished
      // 1     | 0           » unpublish & Save and publish          & Save as unpublished
      // 0     | 1           » publish   & Save and keep published   & Save and unpublish
      // 0     | 0           » unpublish & Save and keep unpublished & Save and publish
      // Add a "Publish" button.
      $element['publish'] = $element['submit'];

      // If the "Publish" button is clicked, we want to update the status to "published".
      $element['publish']['#published_status'] = TRUE;
      $element['publish']['#dropbutton'] = 'save';
      if ($support_ticket
        ->isNew()) {
        $element['publish']['#value'] = t('Save and publish');
      }
      else {
        $element['publish']['#value'] = $support_ticket
          ->isPublished() ? t('Save and keep published') : t('Save and publish');
      }
      $element['publish']['#weight'] = 0;

      // Add a "Unpublish" button.
      $element['unpublish'] = $element['submit'];

      // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
      $element['unpublish']['#published_status'] = FALSE;
      $element['unpublish']['#dropbutton'] = 'save';
      if ($support_ticket
        ->isNew()) {
        $element['unpublish']['#value'] = t('Save as unpublished');
      }
      else {
        $element['unpublish']['#value'] = !$support_ticket
          ->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
      }
      $element['unpublish']['#weight'] = 10;

      // If already published, the 'publish' button is primary.
      if ($support_ticket
        ->isPublished()) {
        unset($element['unpublish']['#button_type']);
      }
      else {
        unset($element['publish']['#button_type']);
        $element['unpublish']['#weight'] = -10;
      }

      // Remove the "Save" button.
      $element['submit']['#access'] = FALSE;
    }
    $element['preview'] = array(
      '#type' => 'submit',
      '#access' => $preview_mode != DRUPAL_DISABLED,
      '#value' => t('Preview'),
      '#weight' => 20,
      '#submit' => array(
        '::submitForm',
        '::preview',
      ),
    );
    $element['delete']['#weight'] = 100;
    return $element;
  }

  /**
   * {@inheritdoc}
   *
   * Updates the support_ticket object by processing the submitted values.
   *
   * This function can be called by a "Next" button of a wizard to update the
   * form state's entity with the current step's values before proceeding to the
   * next step.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Build the support_ticket object from the submitted values.
    parent::submitForm($form, $form_state);
    $support_ticket = $this->entity;

    // Save as a new revision if requested to do so.
    if (!$form_state
      ->isValueEmpty('revision') && $form_state
      ->getValue('revision') != FALSE) {
      $support_ticket
        ->setNewRevision();

      // If a new revision is created, save the current user as revision author.
      $support_ticket
        ->setRevisionCreationTime(REQUEST_TIME);
      $support_ticket
        ->setRevisionAuthorId(\Drupal::currentUser()
        ->id());
    }
    else {
      $support_ticket
        ->setNewRevision(FALSE);
    }
  }

  /**
   * Form submission handler for the 'preview' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
   *   The current state of the form.
   */
  public function preview(array $form, FormStateInterface $form_state) {
    $store = $this->tempStoreFactory
      ->get('support_ticket_preview');
    $this->entity->in_preview = TRUE;
    $store
      ->set($this->entity
      ->uuid(), $form_state);
    $form_state
      ->setRedirect('entity.support_ticket.preview', array(
      'support_ticket_preview' => $this->entity
        ->uuid(),
      'view_mode_id' => 'default',
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\support_ticket\SupportTicketInterface $entity */
    $entity = parent::buildEntity($form, $form_state);
    if (!empty($form_state
      ->getValue('uid')[0]['target_id']) && ($account = User::load($form_state
      ->getValue('uid')[0]['target_id']))) {
      $entity
        ->setOwnerId($account
        ->id());
    }
    else {
      $entity
        ->setOwnerId(0);
    }
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $support_ticket = $this->entity;
    $insert = $support_ticket
      ->isNew();
    $support_ticket
      ->save();
    $support_ticket_link = $support_ticket
      ->link($this
      ->t('View'));
    $context = array(
      '@support_ticket_type' => $support_ticket
        ->getType(),
      '%title' => $support_ticket
        ->label(),
      'link' => $support_ticket_link,
    );
    $t_args = array(
      '@support_ticket_type' => support_ticket_get_type_label($support_ticket),
      '%title' => $support_ticket
        ->label(),
    );
    if ($insert) {
      $this
        ->logger('content')
        ->notice('@support_ticket_type: added %title.', $context);
      drupal_set_message(t('@support_ticket_type %title has been created.', $t_args));
    }
    else {
      $this
        ->logger('content')
        ->notice('@support_ticket_type: updated %title.', $context);
      drupal_set_message(t('@support_ticket_type %title has been updated.', $t_args));
    }
    if ($support_ticket
      ->id()) {
      $form_state
        ->setValue('stid', $support_ticket
        ->id());
      $form_state
        ->set('stid', $support_ticket
        ->id());
      $form_state
        ->setRedirect('entity.support_ticket.canonical', array(
        'support_ticket' => $support_ticket
          ->id(),
      ));

      // Remove the preview entry from the temp store, if any.
      $store = $this->tempStoreFactory
        ->get('support_ticket_preview');
      $store
        ->delete($support_ticket
        ->uuid());
    }
    else {

      // In the unlikely case something went wrong on save, the support_ticket will be
      // rebuilt and support_ticket form redisplayed the same way as in preview.
      drupal_set_message(t('The post could not be saved.'), 'error');
      $form_state
        ->setRebuild();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityForm::$entity protected property The entity being used by this form. Overrides EntityForm::$entity 9
ContentEntityForm::$entityRepository protected property The entity repository service.
ContentEntityForm::$entityTypeBundleInfo protected property The entity type bundle info service.
ContentEntityForm::$time protected property The time service.
ContentEntityForm::addRevisionableFormFields protected function Add revision form fields if the entity enabled the UI.
ContentEntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides EntityForm::copyFormValuesToEntity
ContentEntityForm::flagViolations protected function Flags violations for the current form. 4
ContentEntityForm::getBundleEntity protected function Returns the bundle entity of the entity, or NULL if there is none.
ContentEntityForm::getEditedFieldNames protected function Gets the names of all fields edited in the form. 4
ContentEntityForm::getFormDisplay public function Gets the form display. Overrides ContentEntityFormInterface::getFormDisplay
ContentEntityForm::getFormLangcode public function Gets the code identifying the active form language. Overrides ContentEntityFormInterface::getFormLangcode
ContentEntityForm::getNewRevisionDefault protected function Should new revisions created on default.
ContentEntityForm::init protected function Initializes the form state and the entity before the first form build. Overrides EntityForm::init 1
ContentEntityForm::initFormLangcodes protected function Initializes form language code values.
ContentEntityForm::isDefaultFormLangcode public function Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface::isDefaultFormLangcode
ContentEntityForm::setFormDisplay public function Sets the form display. Overrides ContentEntityFormInterface::setFormDisplay
ContentEntityForm::showRevisionUi protected function Checks whether the revision form fields should be added to the form.
ContentEntityForm::updateChangedTime public function Updates the changed time of the entity.
ContentEntityForm::updateFormLangcode public function Updates the form language to reflect any change to the entity language.
ContentEntityForm::validateForm public function Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level… Overrides FormBase::validateForm 3
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
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
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.
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.
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.
SupportTicketForm::$hasBeenPreviewed protected property Whether this support ticket has been previewed or not.
SupportTicketForm::$tempStoreFactory protected property The tempstore factory.
SupportTicketForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
SupportTicketForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides ContentEntityForm::buildEntity
SupportTicketForm::create public static function Instantiates a new instance of this class. Overrides ContentEntityForm::create
SupportTicketForm::form public function Gets the actual form array to be built. Overrides ContentEntityForm::form
SupportTicketForm::prepareEntity protected function Prepares the entity object before the form is built first. Overrides ContentEntityForm::prepareEntity
SupportTicketForm::preview public function Form submission handler for the 'preview' action.
SupportTicketForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
SupportTicketForm::submitForm public function Updates the support_ticket object by processing the submitted values. Overrides ContentEntityForm::submitForm
SupportTicketForm::updateStatus function Entity builder updating the support_ticket status with the submitted value.
SupportTicketForm::__construct public function Constructs a ContentEntityForm object. Overrides ContentEntityForm::__construct
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.