Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Controller / CustomFormController.php
Size: Mime:
<?php

namespace Drupal\custom_forms\Controller;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\custom_forms\CustomFormInterface;
use Drupal\custom_forms\CustomFormItem;
use Drupal\custom_forms\CustomFormItemFactory;
use Drupal\custom_forms\Entity\CustomFormSubmissionHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CustomFormController
 *
 * @package Drupal\custom_forms\Controller
 */
class CustomFormController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The factory in charge of CRUD operations for custom form items.
   *
   * @var \Drupal\custom_forms\CustomFormItemFactory
   */
  protected $itemFactory;

  protected $languageManager;

  /**
   * Constructs a CustomFormController object.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   */
  public function __construct(
    DateFormatterInterface $date_formatter,
    RendererInterface $renderer,
    EntityRepositoryInterface $entity_repository = NULL,
    CustomFormItemFactory $item_factory,
    LanguageManagerInterface $language_manager
  ) {
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
    if (!$entity_repository) {
      @trigger_error('The entity.repository service must be passed to CustomFormController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
      $entity_repository = \Drupal::service('entity.repository');
    }
    $this->entityRepository = $entity_repository;
    $this->itemFactory = $item_factory;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('date.formatter'),
      $container->get('renderer'),
      $container->get('entity.repository'),
      $container->get('custom_forms.factory.item'),
      $container->get('language_manager')
    );
  }

  /**
   * Displays a custom form revision.
   *
   * @param int $custom_form_revision
   *   The custom form revision ID.
   *
   * @return array
   *   An array suitable for \Drupal\Core\Render\RendererInterface::render().
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionShow($custom_form_revision) {
    $form = $this->entityTypeManager()->getStorage('custom_form')->loadRevision($custom_form_revision);
    $form = $this->entityRepository->getTranslationFromContext($form);
    $custom_form_view_controller = new CustomFormViewController($this->entityTypeManager(), $this->renderer, $this->currentUser(), $this->entityRepository);
    $page = $custom_form_view_controller->view($form);
    unset($page['custom_forms'][$form->id()]['#cache']);
    return $page;
  }

  /**
   * Page title callback for a custom form revision.
   *
   * @param int $custom_form_revision
   *   The custom form revision ID.
   *
   * @return string
   *   The page title.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionPageTitle($custom_form_revision) {
    /** @var CustomFormInterface $custom_form */
    $custom_form = $this->entityTypeManager()->getStorage('custom_form')->loadRevision($custom_form_revision);
    return $this->t('Revision of %title from %date', ['%title' => $custom_form->label(), '%date' => $this->dateFormatter->format($custom_form->getRevisionCreationTime())]);
  }

  /**
   * Generates an overview table of older revisions of a custom form.
   *
   * @param CustomFormInterface $custom_form
   *   A custom form object.
   *
   * @return array
   *   An array as expected by \Drupal\Core\Render\RendererInterface::render().
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function revisionOverview(CustomFormInterface $custom_form) {
    $account = $this->currentUser();
    $langcode = $custom_form->language()->getId();
    $langname = $custom_form->language()->getName();
    $languages = $custom_form->getTranslationLanguages();
    $has_translations = (count($languages) > 1);
    $custom_form_storage = $this->entityTypeManager()->getStorage('custom_form');
    $type = $custom_form->bundle();

    $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $custom_form->label()]) : $this->t('Revisions for %title', ['%title' => $custom_form->label()]);
    $header = [$this->t('Revision'), $this->t('Operations')];

    $revert_permission = (($account->hasPermission("revert $type revisions") || $account->hasPermission('revert all revisions') || $account->hasPermission('administer custom form')) && $custom_form->access('update'));
    $delete_permission = (($account->hasPermission("delete $type revisions") || $account->hasPermission('delete all revisions') || $account->hasPermission('administer custom form')) && $custom_form->access('delete'));

    $rows = [];
    $default_revision = $custom_form->getRevisionId();
    $current_revision_displayed = FALSE;

    foreach ($this->getRevisionIds($custom_form, $custom_form_storage) as $vid) {
      /** @var CustomFormInterface $revision */
      $revision = $custom_form_storage->loadRevision($vid);
      // Only show revisions that are affected by the language that is being
      // displayed.
      if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
        $username = [
          '#theme' => 'username',
          '#account' => $revision->getRevisionUser(),
        ];

        // Use revision link to link to revisions that are not active.
        $date = $this->dateFormatter->format($revision->revision_timestamp->value, 'short');

        // We treat also the latest translation-affecting revision as current
        // revision, if it was the default revision, as its values for the
        // current language will be the same of the current default revision in
        // this case.
        $is_current_revision = $vid == $default_revision || (!$current_revision_displayed && $revision->wasDefaultRevision());
        if (!$is_current_revision) {
          $link = $this->l($date, new Url('entity.custom_form.revision', ['custom_form' => $custom_form->id(), 'custom_form_revision' => $vid]));
        }
        else {
          $link = $custom_form->toLink($date)->toString();
          $current_revision_displayed = TRUE;
        }

        $row = [];
        $column = [
          'data' => [
            '#type' => 'inline_template',
            '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
            '#context' => [
              'date' => $link,
              'username' => $this->renderer->renderPlain($username),
              'message' => ['#markup' => $revision->revision_log->value, '#allowed_tags' => Xss::getHtmlTagList()],
            ],
          ],
        ];
        // @todo Simplify once https://www.drupal.org/node/2334319 lands.
        $this->renderer->addCacheableDependency($column['data'], $username);
        $row[] = $column;

        if ($is_current_revision) {
          $row[] = [
            'data' => [
              '#prefix' => '<em>',
              '#markup' => $this->t('Current revision'),
              '#suffix' => '</em>',
            ],
          ];

          $rows[] = [
            'data' => $row,
            'class' => ['revision-current'],
          ];
        }
        else {
          $links = [];
          if ($revert_permission) {
            $links['revert'] = [
              'title' => $vid < $custom_form->getRevisionId() ? $this->t('Revert') : $this->t('Set as current revision'),
              'url' => $has_translations ?
                Url::fromRoute('custom_form.revision_revert_translation_confirm', ['custom_form' => $custom_form->id(), 'custom_form_revision' => $vid, 'langcode' => $langcode]) :
                Url::fromRoute('custom_form.revision_revert_confirm', ['custom_form' => $custom_form->id(), 'custom_form_revision' => $vid]),
            ];
          }

          if ($delete_permission) {
            $links['delete'] = [
              'title' => $this->t('Delete'),
              'url' => Url::fromRoute('custom_form.revision_delete_confirm', ['custom_form' => $custom_form->id(), 'custom_form_revision' => $vid]),
            ];
          }

          $row[] = [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ];

          $rows[] = $row;
        }
      }
    }

    $build['custom_form_revisions_table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
      '#attached' => [
        'library' => ['node/drupal.node.admin'], // This just adds highlighting of the current revision.
      ],
      '#attributes' => ['class' => 'custom-form-revision-table'],
    ];

    $build['pager'] = ['#type' => 'pager'];

    return $build;
  }

  /**
   * Gets a list of custom form revision IDs for a specific custom form.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   * @param \Drupal\Core\Entity\Sql\SqlContentEntityStorage $sql_content_entity_storage
   *   The SQL content entity storage handler.
   *
   * @return int[]
   *   custom form revision IDs (in descending order).
   */
  protected function getRevisionIds(CustomFormInterface $custom_form, SqlContentEntityStorage $sql_content_entity_storage) {
    $result = $sql_content_entity_storage->getQuery()
      ->allRevisions()
      ->condition($custom_form->getEntityType()->getKey('id'), $custom_form->id())
      ->sort($custom_form->getEntityType()->getKey('revision'), 'DESC')
      ->pager(50)
      ->execute();
    return array_keys($result);
  }

  /**
   * Provides a generic fields title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity fields page.
   */
  public function fieldsTitle(CustomFormInterface $custom_form) {
    return $this->t('%label fields', ['%label' => $custom_form->label()]);
  }

  /**
   * Provides a generic settings title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity settings page.
   */
  public function settingsTitle(CustomFormInterface $custom_form) {
    return $this->t('Settings for %entity-label', ['%entity-label' => $custom_form->label()]);
  }

  /**
   * Provides a generic clone title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity clone page.
   */
  public function cloneTitle(CustomFormInterface $custom_form) {
    return $this->t('Clone %entity-label', ['%entity-label' => $custom_form->label()]);
  }

  /**
   * Provides a generic add-field title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity add-field page.
   */
  public function addFieldTitle(CustomFormInterface $custom_form) {
    return $this->t('Add field to %entity-label', ['%entity-label' => $custom_form->label()]);
  }

  /**
   * Provides a generic add-group title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity add-group page.
   */
  public function addGroupTitle(CustomFormInterface $custom_form) {
    return $this->t('Add group to %entity-label', ['%entity-label' => $custom_form->label()]);
  }

  /**
   * Provides a generic delete-item title callback.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object for getting the route data.
   *
   * @return string
   *   The title for the entity delete-item page.
   */
  public function deleteItemTitle(RouteMatchInterface $route_match) {
    if ($id = $route_match->getParameter('item')) {
      /** @var \Drupal\custom_forms\CustomFormItem $item */
      $item = $this->itemFactory->loadItem($id);
      return $this->t('Are you sure you want to delete %entity-label?', ['%entity-label' => $item->getSetting('label')]);
    }
  }

  /**
   * Provides a generic translate-item title callback.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object for getting the route data.
   *
   * @return string
   *   The title for the entity delete-item page.
   */
  public function translateItemTitle(RouteMatchInterface $route_match) {
    $id = $route_match->getParameter('item');
    $langcode = $route_match->getParameter('langcode');
    if ($id !== null && $langcode !== null) {
      /** @var \Drupal\custom_forms\CustomFormItem $item */
      $item = $this->itemFactory->loadItem($id);
      $language = $this->languageManager->getLanguage($langcode);
      return $this->t('@language translation of %entity-label', [
        '@language' => ucfirst($language->getName()),
        '%entity-label' => $item->getSetting('label')
      ]);
    }
  }

  /**
   * Provides a generic item-settings title callback.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object for getting the route data.
   *
   * @return string
   *   The title for the entity item-settings page.
   */
  public function itemSettingsTitle(RouteMatchInterface $route_match) {
    if ($id = $route_match->getParameter('item')) {
      /** @var \Drupal\custom_forms\CustomFormItem $field */
      $field = $this->itemFactory->loadItem($id);
      return $this->t('Settings for %entity-label', ['%entity-label' => $field->getSetting('label')]);
    }
  }

  /**
   * Provides a generic item-translat title callback.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object for getting the route data.
   *
   * @return string
   *   The title for the entity item-settings page.
   */
  public function itemTranslationsTitle(RouteMatchInterface $route_match) {
    if ($id = $route_match->getParameter('id')) {
      /** @var \Drupal\custom_forms\CustomFormItem $field */
      $field = $this->itemFactory->loadItem($id);
      return $this->t('Translations of %entity-label', ['%entity-label' => $field->getSetting('label')]);
    }
  }

  /**
   * Provides a generic handlers title callback.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   *
   * @return string
   *   The title for the entity fields page.
   */
  public function handlerListTitle(RouteMatchInterface $route_match) {
    /** @var \Drupal\custom_forms\Entity\CustomForm $custom_form */
    if ($custom_form = $route_match->getParameter('custom_form')) {
      return $this->t('%label handlers', ['%label' => $custom_form->label()]);
    }
  }

  /**
   * Provides a generic handler title callback.
   *
   * @param \Drupal\custom_forms\CustomFormInterface $custom_form
   *   The custom form entity.
   *
   * @return string
   *   The title for the entity settings page.
   */
  public function handlerTitle(CustomFormInterface $custom_form, CustomFormSubmissionHandlerInterface $custom_form_submission_handler) {
    return $this->t('Edit %handler for %form', ['%handler' => $custom_form_submission_handler->label(), '%form' => $custom_form->label()]);
  }
}