You are here

class EntityBreakLockForm in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 src/Form/EntityBreakLockForm.php \Drupal\content_lock\Form\EntityBreakLockForm

Provides a base class for break content lock forms.

Hierarchy

Expanded class hierarchy of EntityBreakLockForm

File

src/Form/EntityBreakLockForm.php, line 20

Namespace

Drupal\content_lock\Form
View source
class EntityBreakLockForm extends FormBase {

  /**
   * Content lock service.
   *
   * @var \Drupal\content_lock\ContentLock\ContentLock
   */
  protected $lockService;

  /**
   * Current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * EntityBreakLockForm constructor.
   *
   * @param \Drupal\content_lock\ContentLock\ContentLock $contentLock
   *   Content lock service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   Request stack service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   Language manager service.
   */
  public function __construct(ContentLock $contentLock, RequestStack $requestStack, LanguageManagerInterface $language_manager) {
    $this->lockService = $contentLock;
    $this->request = $requestStack
      ->getCurrentRequest();
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('content_lock'), $container
      ->get('request_stack'), $container
      ->get('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity_type = $form_state
      ->getValue('entity_type_id');
    $entity_id = $form_state
      ->getValue('entity_id');
    $langcode = $form_state
      ->getValue('langcode');
    $form_op = $form_state
      ->getValue('form_op') ?: NULL;
    $this->lockService
      ->release($entity_id, $langcode, $form_op, NULL, $entity_type);
    if ($form_state
      ->get('translation_lock')) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Lock broken. Anyone can now edit this content translation.'));
    }
    else {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Lock broken. Anyone can now edit this content.'));
    }

    // Redirect URL to the request destination or the canonical entity view.
    if ($destination = $this->request->query
      ->get('destination')) {
      $url = Url::fromUserInput($destination);
      $form_state
        ->setRedirectUrl($url);
    }
    else {
      $language = $this->languageManager
        ->getLanguage($form_state
        ->get('langcode_entity'));
      $url = Url::fromRoute("entity.{$entity_type}.canonical", [
        $entity_type => $entity_id,
      ], [
        'language' => $language,
      ]);
      $form_state
        ->setRedirectUrl($url);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'break_lock_entity';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL, $langcode = NULL, $form_op = NULL) {

    // Save langcode of lock, before checking if translation lock is enabled.
    // This is needed to generate the correct entity URL for the given language.
    $form_state
      ->set('langcode_entity', $langcode);
    $translation_lock = $this->lockService
      ->isTranslationLockEnabled($entity
      ->getEntityTypeId());
    if (!$translation_lock) {
      $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    }
    $form_state
      ->set('translation_lock', $translation_lock);
    $form_op_lock = $this->lockService
      ->isFormOperationLockEnabled($entity
      ->getEntityTypeId());
    if (!$form_op_lock) {
      $form_op = '*';
    }
    $form['#title'] = $this
      ->t('Break Lock for content @label', [
      '@label' => $entity
        ->label(),
    ]);
    $form['entity_id'] = [
      '#type' => 'value',
      '#value' => $entity
        ->id(),
    ];
    $form['entity_type_id'] = [
      '#type' => 'value',
      '#value' => $entity
        ->getEntityTypeId(),
    ];
    $form['langcode'] = [
      '#type' => 'value',
      '#value' => $langcode,
    ];
    $form['form_op'] = [
      '#type' => 'value',
      '#value' => $form_op,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Confirm break lock'),
    ];
    return $form;
  }

  /**
   * Custom access checker for the form route requirements.
   */
  public function access(ContentEntityInterface $entity, $langcode, $form_op, AccountInterface $account) {
    return AccessResult::allowedIf($account
      ->hasPermission('break content lock') || $this->lockService
      ->isLockedBy($entity
      ->id(), $langcode, $form_op, $account
      ->id(), $entity
      ->getEntityTypeId()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
EntityBreakLockForm::$languageManager protected property Language manager service.
EntityBreakLockForm::$lockService protected property Content lock service.
EntityBreakLockForm::$request protected property Current request.
EntityBreakLockForm::access public function Custom access checker for the form route requirements.
EntityBreakLockForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EntityBreakLockForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EntityBreakLockForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EntityBreakLockForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityBreakLockForm::__construct public function EntityBreakLockForm constructor.
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.
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.