You are here

class AjaxController in Sortableviews 8

Controller for sortableviews ajax calls.

Hierarchy

Expanded class hierarchy of AjaxController

1 file declares its use of AjaxController
AjaxControllerTest.php in tests/src/Unit/Controller/AjaxControllerTest.php

File

src/Controller/AjaxController.php, line 17

Namespace

Drupal\sortableviews\Controller
View source
class AjaxController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * An instance of the entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;

  /**
   * Builds a new AjaxController object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

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

  /**
   * Returns the entity order adjusted for the view pager.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return array
   *   An array with the adjusted sort order where the key
   *   is the entity weight and the value, the entity id.
   */
  protected function retrieveOrderFromRequest(Request $request) {

    // Adjust order for pager in asc fashion.
    if ($request
      ->get('items_per_page') && $request
      ->get('sort_order') == 'asc') {
      $adjusted_order = [];
      foreach ($request
        ->get('current_order') as $index => $value) {
        $new_index = $index + $request
          ->get('page_number') * $request
          ->get('items_per_page');
        $adjusted_order[$new_index] = $value;
      }
      return $adjusted_order;
    }

    // Adjust order for pager in desc fashion.
    if ($request
      ->get('items_per_page') && $request
      ->get('sort_order') == 'desc') {
      $complete_pages = (int) ($request
        ->get('total_rows') / $request
        ->get('items_per_page'));
      if ($complete_pages == $request
        ->get('page_number')) {

        // This is the last page of the view.
        return $request
          ->get('current_order');
      }
      $adjusted_order = [];
      $mod = (int) $request
        ->get('total_rows') % $request
        ->get('items_per_page');
      foreach ($request
        ->get('current_order') as $index => $value) {
        $new_index = $index + ($complete_pages - ($request
          ->get('page_number') + 1)) * $request
          ->get('items_per_page') + $mod;
        $adjusted_order[$new_index] = $value;
      }
      return $adjusted_order;
    }
    return $request
      ->get('current_order');
  }

  /**
   * Saves new weights.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An response with Ajax commands.
   */
  public function ajaxSave(Request $request) {
    $entity_type = $request
      ->get('entity_type');
    $field = $request
      ->get('weight_field');
    $current_order = $this
      ->retrieveOrderFromRequest($request);
    $entities = $this->entityManager
      ->getStorage($entity_type)
      ->loadMultiple(array_values($current_order));
    foreach ($entities as $entity) {
      $entity
        ->set($field, array_search($entity
        ->id(), $current_order));
      $entity
        ->save();
    }
    $content = [
      '#type' => 'container',
      '#attributes' => [
        'class' => 'status-messages',
      ],
      'messages' => [
        '#theme' => 'status_messages',
        '#message_list' => [
          'status' => [
            $this
              ->t('Changes have been saved.'),
          ],
        ],
      ],
    ];
    $response = new AjaxResponse();
    $response
      ->addCommand(new PrependCommand('.js-view-dom-id-' . $request
      ->get('dom_id'), $content));
    $response
      ->addCommand(new RemoveCommand('.js-view-dom-id-' . $request
      ->get('dom_id') . ' .sortableviews-ajax-trigger'));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxController::$entityManager protected property An instance of the entity manager service.
AjaxController::ajaxSave public function Saves new weights.
AjaxController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AjaxController::retrieveOrderFromRequest protected function Returns the entity order adjusted for the view pager.
AjaxController::__construct public function Builds a new AjaxController object.
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.