You are here

ParagraphsItemController.php in Paragraphs table 8

File

src/Controller/ParagraphsItemController.php
View source
<?php

namespace Drupal\paragraphs_table\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\paragraphs\Entity\Paragraph;

/**
 * Returns responses for paragraphs item routes.
 */
class ParagraphsItemController extends ControllerBase {

  /**
   * Provides the paragraphs item submission form.
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The paragraphs entity for the paragraph item.
   * @param string $host_type
   *   The type of the entity hosting the paragraph item.
   * @param int $host_id
   *   The id of the entity hosting the paragraph item.
   *
   * @return array
   *   A paragraph item submission form.
   *
   *   @todo additional fields
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function add(Paragraph $paragraph, $host_type, $host_id) {
    $host = $this
      ->entityTypeManager()
      ->getStorage($host_type)
      ->load($host_id);
    $paragraphs_item = $this
      ->entityTypeManager()
      ->getStorage('paragraph')
      ->create([
      'field_name' => $paragraph
        ->id(),
      'type' => $host_type,
      'revision_id' => 0,
    ]);
    return $this
      ->entityFormBuilder()
      ->getForm($paragraphs_item);
  }

  /**
   * The _title_callback for the paragraphs_item.add route.
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The current paragraphs_item.
   *
   * @return string
   *   The page title.
   */
  public function addPageTitle(Paragraph $paragraph) {
    return $this
      ->t('Create @label', [
      '@label' => $paragraph
        ->label(),
    ]);
  }

  /**
   * Displays a paragraphs item.
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The Paragraph item we are displaying.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function page(Paragraph $paragraph) {
    return $this
      ->buildPage($paragraph);
  }

  /**
   * The _title_callback for the paragraphs_item.view route.
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The current paragraphs_item.
   *
   * @return string
   *   The page title.
   */
  public function pageTitle(Paragraph $paragraph = NULL) {
    return \Drupal::service('entity.repository')
      ->getTranslationFromContext($paragraph)
      ->label() . ' #' . $paragraph
      ->id();
  }

  /**
   * Builds a paragraph item page render array.
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The field paragraph item we are displaying.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  protected function buildPage(Paragraph $paragraph) {
    return [
      'paragraph' => $this
        ->entityTypeManager()
        ->getViewBuilder('paragraph')
        ->view($paragraph),
    ];
  }

}

Classes

Namesort descending Description
ParagraphsItemController Returns responses for paragraphs item routes.