You are here

class DynamicLayoutController in Dynamic Layouts 8

DynamicLayoutController class.

Hierarchy

Expanded class hierarchy of DynamicLayoutController

File

src/Controller/DynamicLayoutController.php, line 19

Namespace

Drupal\dynamic_layouts\Controller
View source
class DynamicLayoutController extends ControllerBase {

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilder
   */
  protected $formBuilder;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity form builder.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

  /**
   * EditRowModalForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   *   The entity form builder.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Form\FormBuilder $formBuilder
   *   The form builder.
   */
  public function __construct(EntityFormBuilderInterface $entity_form_builder, EntityTypeManagerInterface $entityTypeManager, FormBuilder $formBuilder) {
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFormBuilder = $entity_form_builder;
    $this->formBuilder = $formBuilder;
  }

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

  /**
   * Checks access to add a new layout.
   */
  public function access() {
    $settings_created = FALSE;

    /** @var \Drupal\dynamic_layouts\DynamicLayoutSettingsInterface $settings */
    if (($settings = $this->entityTypeManager
      ->getStorage('dynamic_layout_settings')
      ->load('settings')) && $settings
      ->getFrontendLibrary()) {
      $settings_created = TRUE;
    }
    return AccessResult::allowedIf($settings_created);
  }

  /**
   * Callback for deleting a column.
   *
   * @param string $dynamic_layout_id
   *   The dynamic layout id.
   * @param int $column_id
   *   The column id we need to delete.
   * @param int $row_id
   *   The row number we need to delete the column.
   *
   * @return object
   *   The ajax response.
   */
  public function deleteColumn($dynamic_layout_id, $column_id, $row_id) {
    $response = new AjaxResponse();

    /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $config_entity */
    if ($config_entity = $this->entityTypeManager
      ->getStorage(Constants::DYNAMIC_LAYOUT)
      ->load($dynamic_layout_id)) {

      // Delete the specific column from the config entity.
      $config_entity
        ->deleteColumn($row_id, $column_id);

      // Save the entity.
      $config_entity
        ->save();

      // Replace the layout form with newly updated values.

      /* @var \Drupal\dynamic_layouts\Form\DynamicLayoutForm $layout_form */
      $layout_form = $this->entityFormBuilder
        ->getForm($config_entity);
      $response
        ->addCommand(new ReplaceCommand(Constants::DYNAMIC_LAYOUT_FORM_CLASS, $layout_form));
    }
    return $response;
  }

  /**
   * Callback for adding a column.
   *
   * @param string $dynamic_layout_id
   *   The dynamic layout id.
   * @param int $row_id
   *   The row number we need to delete the column.
   *
   * @return object
   *   The ajax response.
   */
  public function addColumn($dynamic_layout_id, $row_id) {
    $response = new AjaxResponse();

    /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $config_entity */
    if ($config_entity = $this->entityTypeManager
      ->getStorage(Constants::DYNAMIC_LAYOUT)
      ->load($dynamic_layout_id)) {

      // Add a column to the config entity.
      $config_entity
        ->addColumn($row_id);

      // Save the entity.
      $config_entity
        ->save();

      // Replace the layout form with newly updated values.

      /* @var \Drupal\dynamic_layouts\Form\DynamicLayoutForm $layout_form */
      $layout_form = $this->entityFormBuilder
        ->getForm($config_entity);
      $response
        ->addCommand(new ReplaceCommand(Constants::DYNAMIC_LAYOUT_FORM_CLASS, $layout_form));
    }
    return $response;
  }

  /**
   * Callback for opening the modal form.
   *
   * @param string $dynamic_layout_id
   *   The dynamic layout id.
   * @param int $row_id
   *   The row number we need to delete.
   *
   * @return object
   *   The ajax response.
   */
  public function deleteRow($dynamic_layout_id, $row_id) {
    $response = new AjaxResponse();

    /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $config_entity */
    if ($config_entity = $this->entityTypeManager
      ->getStorage(Constants::DYNAMIC_LAYOUT)
      ->load($dynamic_layout_id)) {

      // Delete the specific row from the config entity.
      $config_entity
        ->deleteRow($row_id);

      // Save the entity.
      $config_entity
        ->save();

      // Replace the layout form with newly updated values.

      /* @var \Drupal\dynamic_layouts\Form\DynamicLayoutForm $layout_form */
      $layout_form = $this->entityFormBuilder
        ->getForm($config_entity);
      $response
        ->addCommand(new ReplaceCommand(Constants::DYNAMIC_LAYOUT_FORM_CLASS, $layout_form));
    }
    return $response;
  }

  /**
   * Callback for adding a row.
   *
   * @param string $dynamic_layout_id
   *   The dynamic layout id.
   *
   * @return object
   *   The ajax response.
   */
  public function addRow($dynamic_layout_id) {
    $response = new AjaxResponse();

    /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $config_entity */
    if ($config_entity = $this->entityTypeManager
      ->getStorage(Constants::DYNAMIC_LAYOUT)
      ->load($dynamic_layout_id)) {

      // Add a row.
      $config_entity
        ->addRow();

      // Save the entity.
      $config_entity
        ->save();

      // Replace the layout form with newly updated values.

      /* @var \Drupal\dynamic_layouts\Form\DynamicLayoutForm $layout_form */
      $layout_form = $this->entityFormBuilder
        ->getForm($config_entity);
      $response
        ->addCommand(new ReplaceCommand(Constants::DYNAMIC_LAYOUT_FORM_CLASS, $layout_form));
    }
    return $response;
  }

  /**
   * Callback for opening the edit column modal form.
   */
  public function openEditColumnModalForm() {
    $response = new AjaxResponse();

    // Get the modal form using the form builder.
    $modal_form = $this->formBuilder
      ->getForm('Drupal\\dynamic_layouts\\Form\\EditColumnModalForm');

    // Add an AJAX command to open a modal dialog with the form as the content.
    $response
      ->addCommand(new OpenModalDialogCommand('Edit column', $modal_form, [
      'width' => '800',
    ]));
    return $response;
  }

  /**
   * Callback for opening the modal form.
   */
  public function openEditRowModalForm() {
    $response = new AjaxResponse();

    // Get the modal form using the form builder.
    $modal_form = $this->formBuilder
      ->getForm('Drupal\\dynamic_layouts\\Form\\EditRowModalForm');

    // Add an AJAX command to open a modal dialog with the form as the content.
    $response
      ->addCommand(new OpenModalDialogCommand('Edit row', $modal_form, [
      'width' => '800',
    ]));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityManager protected property The entity manager.
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::entityManager Deprecated protected function Retrieves the entity manager service.
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. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DynamicLayoutController::$entityFormBuilder protected property The entity form builder. Overrides ControllerBase::$entityFormBuilder
DynamicLayoutController::$entityTypeManager protected property The entity type manager. Overrides ControllerBase::$entityTypeManager
DynamicLayoutController::$formBuilder protected property The form builder. Overrides ControllerBase::$formBuilder
DynamicLayoutController::access public function Checks access to add a new layout.
DynamicLayoutController::addColumn public function Callback for adding a column.
DynamicLayoutController::addRow public function Callback for adding a row.
DynamicLayoutController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
DynamicLayoutController::deleteColumn public function Callback for deleting a column.
DynamicLayoutController::deleteRow public function Callback for opening the modal form.
DynamicLayoutController::openEditColumnModalForm public function Callback for opening the edit column modal form.
DynamicLayoutController::openEditRowModalForm public function Callback for opening the modal form.
DynamicLayoutController::__construct public function EditRowModalForm constructor.
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.