You are here

class ProjectController in Drupal PM (Project Management) 4.x

Class ProjectController.

Returns responses for Project routes.

Hierarchy

Expanded class hierarchy of ProjectController

File

modules/pm_project/src/Controller/ProjectController.php, line 18

Namespace

Drupal\pm_project\Controller
View source
class ProjectController extends ControllerBase implements ContainerInjectionInterface {

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

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->dateFormatter = $container
      ->get('date.formatter');
    $instance->renderer = $container
      ->get('renderer');
    return $instance;
  }

  /**
   * Displays a Project revision.
   *
   * @param int $pm_project_revision
   *   The Project revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function revisionShow($pm_project_revision) {
    $pm_project = $this
      ->entityTypeManager()
      ->getStorage('pm_project')
      ->loadRevision($pm_project_revision);
    $view_builder = $this
      ->entityTypeManager()
      ->getViewBuilder('pm_project');
    return $view_builder
      ->view($pm_project);
  }

  /**
   * Page title callback for a Project revision.
   *
   * @param int $pm_project_revision
   *   The Project revision ID.
   *
   * @return string
   *   The page title.
   */
  public function revisionPageTitle($pm_project_revision) {
    $pm_project = $this
      ->entityTypeManager()
      ->getStorage('pm_project')
      ->loadRevision($pm_project_revision);
    return $this
      ->t('Revision of %title from %date', [
      '%title' => $pm_project
        ->label(),
      '%date' => $this->dateFormatter
        ->format($pm_project
        ->getRevisionCreationTime()),
    ]);
  }

  /**
   * Generates an overview table of older revisions of a Project.
   *
   * @param \Drupal\pm_project\Entity\ProjectInterface $pm_project
   *   A Project object.
   *
   * @return array
   *   An array as expected by drupal_render().
   */
  public function revisionOverview(ProjectInterface $pm_project) {
    $account = $this
      ->currentUser();
    $pm_project_storage = $this
      ->entityTypeManager()
      ->getStorage('pm_project');
    $build['#title'] = $this
      ->t('Revisions for %title', [
      '%title' => $pm_project
        ->label(),
    ]);
    $header = [
      $this
        ->t('Revision'),
      $this
        ->t('Operations'),
    ];
    $revert_permission = $account
      ->hasPermission("revert all project revisions") || $account
      ->hasPermission('administer project entities');
    $delete_permission = $account
      ->hasPermission("delete all project revisions") || $account
      ->hasPermission('administer project entities');
    $rows = [];
    $vids = $pm_project_storage
      ->revisionIds($pm_project);
    $latest_revision = TRUE;
    foreach (array_reverse($vids) as $vid) {

      /** @var \Drupal\pm_project\ProjectInterface $revision */
      $revision = $pm_project_storage
        ->loadRevision($vid);
      $username = [
        '#theme' => 'username',
        '#account' => $revision
          ->getRevisionUser(),
      ];

      // Use revision link to link to revisions that are not active.
      $date = $this->dateFormatter
        ->format($revision
        ->getRevisionCreationTime(), 'short');
      if ($vid != $pm_project
        ->getRevisionId()) {
        $link = Link::fromTextAndUrl($date, new Url('entity.pm_project.revision', [
          'pm_project' => $pm_project
            ->id(),
          'pm_project_revision' => $vid,
        ]));
      }
      else {
        $link = $pm_project
          ->toLink()
          ->toString($date);
      }
      $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
                ->getRevisionLogMessage(),
              '#allowed_tags' => Xss::getHtmlTagList(),
            ],
          ],
        ],
      ];
      $row[] = $column;
      if ($latest_revision) {
        $row[] = [
          'data' => [
            '#prefix' => '<em>',
            '#markup' => $this
              ->t('Current revision'),
            '#suffix' => '</em>',
          ],
        ];
        foreach ($row as &$current) {
          $current['class'] = [
            'revision-current',
          ];
        }
        $latest_revision = FALSE;
      }
      else {
        $links = [];
        if ($revert_permission) {
          $links['revert'] = [
            'title' => $this
              ->t('Revert'),
            'url' => Url::fromRoute('entity.pm_project.revision_revert', [
              'pm_project' => $pm_project
                ->id(),
              'pm_project_revision' => $vid,
            ]),
          ];
        }
        if ($delete_permission) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.pm_project.revision_delete', [
              'pm_project' => $pm_project
                ->id(),
              'pm_project_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
      }
      $rows[] = $row;
    }
    $build['pm_project_revisions_table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage 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. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
ProjectController::$dateFormatter protected property The date formatter.
ProjectController::$renderer protected property The renderer.
ProjectController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ProjectController::revisionOverview public function Generates an overview table of older revisions of a Project.
ProjectController::revisionPageTitle public function Page title callback for a Project revision.
ProjectController::revisionShow public function Displays a Project revision.
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. 4
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.