Commit b8ba4d26 authored by iStryker's avatar iStryker Committed by tstruyk1

Issue #2917563 by iStryker: Update coding standard

parent 9887d7d4
......@@ -3,7 +3,7 @@ type: module
description: 'Allows your views to be draggable and gives you the ability to set the order of how they appear.'
core: 8.x
dependencies:
- views
- drupal:views
test_dependencies:
- draggableviews:draggableviews_demo
package: Views
......@@ -5,6 +5,8 @@
* Install, update and uninstall functions for the draggableviews module.
*/
use Drupal\views\Views;
/**
* Implements hook_schema().
*/
......@@ -70,3 +72,42 @@ function draggableviews_schema() {
return $schema;
}
/**
* Implements hook_update().
*/
function draggableviews_update_8104(&$sandbox) {
$views = Views::getEnabledViews();
foreach ($views as $view) {
$source_value = '';
$config = \Drupal::config('views.view.' . $view->id());
$rawData = $config->getRawData();
$master = FALSE;
// Get the display with the draggableviews field.
// It is a little tricky because the 'default' display is the master.
foreach ($rawData['display'] as $display_key => $display) {
$field = isset($display['display_options']['fields']) ? $display['display_options']['fields'] : [];
if ($display_key == 'default' && array_key_exists('draggableviews', $field)) {
$master = TRUE;
}
if ($display_key != 'default' && array_key_exists('draggableviews', $field)) {
$source_value = $view->id() . '|' . $display_key;
}
if ($master && empty($display['display_options']['fields'])) {
$source_value = $view->id() . '|' . $display_key;
}
}
if ($source_value) {
// Save the view with the source value in the sort handler.
foreach ($rawData['display'] as $display_key => $display) {
$sorts = isset($display['display_options']['sorts']) ? $display['display_options']['sorts'] : [];
if (array_key_exists('weight', $sorts)) {
$view_exec = Views::getView($view->id());
$view_exec->setHandlerOption($display_key, 'sort', 'weight', 'source', $source_value);
$view_exec->save();
}
}
}
}
}
......@@ -5,6 +5,11 @@
* Contains draggableviews.module.
*/
use Drupal\draggableviews\DraggableViews;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Cache\Cache;
/**
* Implements hook_views_data_alter().
*/
......@@ -17,7 +22,7 @@ function draggableviews_views_data_alter(&$data) {
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
'id' => 'draggableviews_sort',
],
'filter' => [
'help' => t('Filter by the draggableviews weight value (Native handler only).'),
......@@ -32,12 +37,12 @@ function draggableviews_views_data_alter(&$data) {
'id' => 'numeric',
],
'filter' => [
'help' => t('Filter by the draggableviews parent\'s entity id (Native handler only).'),
'help' => t("Filter by the draggableviews parent's entity id (Native handler only)."),
'id' => 'numeric',
],
];
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) {
$base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
$entity_keys = $entity_type->getKeys();
if ($base_table && isset($data[$base_table]['table'])) {
......@@ -51,7 +56,8 @@ function draggableviews_views_data_alter(&$data) {
'click sortable' => FALSE,
],
];
// Explain to every entity how to join with draggableviews structure table.
// Explain to every entity how to join with draggableviews_structure
// table.
$data['draggableviews_structure']['table']['join'][$base_table] = [
'handler' => 'draggableviews_join_handler',
// Because this is a direct link it could be left out.
......@@ -74,7 +80,7 @@ function draggableviews_preprocess_views_view_table(&$variables) {
return;
}
$draggableviews = new \Drupal\draggableviews\DraggableViews($variables['view']);
$draggableviews = new DraggableViews($variables['view']);
// Add hierarchy.
foreach ($variables['rows'] as $key => $row) {
......@@ -85,7 +91,7 @@ function draggableviews_preprocess_views_view_table(&$variables) {
if ($first_column !== 'draggableviews') {
break;
}
// Set the
// Set the first column.
$first_column = each($columns);
} while ($first_column);
......@@ -95,7 +101,7 @@ function draggableviews_preprocess_views_view_table(&$variables) {
'#theme' => 'indentation',
'#size' => $draggableviews->getDepth($key),
];
$variables['rows'][$key]['columns'][$first_column]['content'][0]['field_output']['#markup'] = (string)(render($indent) . $columns_title);
$variables['rows'][$key]['columns'][$first_column]['content'][0]['field_output']['#markup'] = (string) (render($indent) . $columns_title);
}
// Add table attributes.
......@@ -111,7 +117,7 @@ function draggableviews_preprocess_views_view_table(&$variables) {
/**
* Implements hook_form_alter().
*/
function draggableviews_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
function draggableviews_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Filter the right form.
if (strpos($form_id, 'views_form_') === FALSE) {
return;
......@@ -146,7 +152,7 @@ function draggableviews_form_alter(&$form, \Drupal\Core\Form\FormStateInterface
/**
* Submit handler.
*/
function draggableviews_views_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
function draggableviews_views_submit(&$form, FormStateInterface $form_state) {
$input = $form_state->getUserInput();
/** @var \Drupal\views\ViewExecutable $view */
......@@ -156,7 +162,7 @@ function draggableviews_views_submit(&$form, \Drupal\Core\Form\FormStateInterfac
$weight = 0;
$connection = \Drupal\Core\Database\Database::getConnection();
$connection = Database::getConnection();
$transaction = $connection->startTransaction();
try {
foreach ($input['draggableviews'] as $item) {
......@@ -186,12 +192,12 @@ function draggableviews_views_submit(&$form, \Drupal\Core\Form\FormStateInterfac
// cache.
$views_entity_table_info = $view->query->getEntityTableInfo();
// Find the entity type used by the view.
$result = array_keys(array_filter($views_entity_table_info, function($info) {
$result = array_keys(array_filter($views_entity_table_info, function ($info) {
return $info['relationship_id'] == 'none';
}));
$entity_type_id = reset($result);
$list_cache_tags = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getListCacheTags();
\Drupal\Core\Cache\Cache::invalidateTags($list_cache_tags);
Cache::invalidateTags($list_cache_tags);
}
catch (\Exception $e) {
$transaction->rollback();
......
......@@ -16,4 +16,3 @@ help: ''
new_revision: true
preview_mode: 1
display_submitted: true
......@@ -4,7 +4,7 @@ description: 'Demo module for Draggableviews.'
core: 8.x
dependencies:
- drupal:block
- draggableviews
- drupal:draggableviews
- drupal:menu_ui
- drupal:node
- drupal:user
......
<?php
use Drupal\node\Entity\Node;
/**
* @file
* Install, update and uninstall functions for the draggableviews demo module.
*/
use Drupal\node\Entity\Node;
/**
* Implements hook_uninstall().
*/
......
......@@ -13,7 +13,7 @@ class DraggableViews {
/**
* The view.
*
* @var \Drupal\views\ViewExecutable $view
* @var \Drupal\views\ViewExecutable
*/
public $view;
......
......@@ -91,4 +91,5 @@ class DraggableViewsField extends BulkForm {
drupal_attach_tabledrag($form, $options);
}
}
}
......@@ -3,21 +3,24 @@
namespace Drupal\Tests\draggableviews\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\Node;
/**
* Tests sortability of Draggableviewws.
*
* @group draggableviews
*/
class DraggableviewsTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'views', 'draggableviews', 'draggableviews_demo'];
public static $modules = [
'node',
'views',
'draggableviews',
'draggableviews_demo',
];
/**
* The installation profile to use with this test.
......@@ -45,8 +48,8 @@ class DraggableviewsTest extends BrowserTestBase {
// Gather the test data.
$dataContent = $this->providerTestDataContent();
// Create nodes.
// Create nodes.
foreach ($dataContent as $datumContent) {
$node = $this->drupalCreateNode([
'type' => 'draggableviews_demo',
......@@ -57,12 +60,12 @@ class DraggableviewsTest extends BrowserTestBase {
}
/**
* Data provider for setUp
* Data provider for setUp.
*
* @return array
* Nested array of testing data, Arranged like this:
* - Title
* - Body
* Nested array of testing data, Arranged like this:
* - Title
* - Body
*/
protected function providerTestDataContent() {
return [
......@@ -85,21 +88,22 @@ class DraggableviewsTest extends BrowserTestBase {
[
'Draggable Content 5',
'Draggable Content Body 5',
]
],
];
}
/**
* A simple test.
*/
public function testDraggableviewsContent() {
$assert_session = $this->assertSession();
$this->drupalGet('draggableviews-demo');
$this->assertSession()->statusCodeEquals(200);
// Verify that anonymous useres cannot access the order page.
$this->drupalGet('draggableviews-demo/order');
$this->assertSession()->statusCodeEquals(403);
// Verify that authorized user has access to display page.
$this->drupalLogin($this->adminUser);
$this->drupalGet('draggableviews-demo');
......@@ -111,8 +115,9 @@ class DraggableviewsTest extends BrowserTestBase {
// Verify that authorized user has access to order page.
$this->drupalGet('draggableviews-demo/order');
$this->assertSession()->statusCodeEquals(200);
// Verify that the page contains generated content.
$assert_session->pageTextContains(t('Draggable Content 5'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment