Commit 887eea4e authored by alexpott's avatar alexpott

Issue #2317085 by pfrenssen | rteijeiro: Added the possibility to create a...

Issue #2317085 by pfrenssen | rteijeiro: Added the possibility to create a REST export when creating a new view.
parent 95019fa6
......@@ -370,73 +370,113 @@ public function buildForm(array $form, FormStateInterface $form_state) {
);
}
if (!\Drupal::moduleHandler()->moduleExists('block')) {
return $form;
}
$form['displays']['block'] = array(
'#type' => 'fieldset',
'#title' => t('Block settings'),
'#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend')),
'#tree' => TRUE,
);
$form['displays']['block']['create'] = array(
'#title' => t('Create a block'),
'#type' => 'checkbox',
'#attributes' => array('class' => array('strong')),
'#id' => 'edit-block-create',
);
// Only offer the block settings if the module is enabled.
if (\Drupal::moduleHandler()->moduleExists('block')) {
$form['displays']['block'] = array(
'#type' => 'fieldset',
'#title' => t('Block settings'),
'#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend')),
'#tree' => TRUE,
);
$form['displays']['block']['create'] = array(
'#title' => t('Create a block'),
'#type' => 'checkbox',
'#attributes' => array('class' => array('strong')),
'#id' => 'edit-block-create',
);
// All options for the block display are included in this container so they
// can be hidden as a group when the "Create a page" checkbox is unchecked.
$form['displays']['block']['options'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('options-set')),
'#states' => array(
'visible' => array(
':input[name="block[create]"]' => array('checked' => TRUE),
// All options for the block display are included in this container so
// they can be hidden as a group when the "Create a block" checkbox is
// unchecked.
$form['displays']['block']['options'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('options-set')),
'#states' => array(
'visible' => array(
':input[name="block[create]"]' => array('checked' => TRUE),
),
),
),
'#prefix' => '<div id="edit-block-wrapper">',
'#suffix' => '</div>',
'#parents' => array('block'),
);
'#prefix' => '<div id="edit-block-wrapper">',
'#suffix' => '</div>',
'#parents' => array('block'),
);
$form['displays']['block']['options']['title'] = array(
'#title' => t('Block title'),
'#type' => 'textfield',
'#maxlength' => 255,
);
$form['displays']['block']['options']['style'] = array(
'#type' => 'fieldset',
'#title' => t('Block display settings'),
'#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
);
$form['displays']['block']['options']['title'] = array(
'#title' => t('Block title'),
'#type' => 'textfield',
'#maxlength' => 255,
);
$form['displays']['block']['options']['style'] = array(
'#type' => 'fieldset',
'#title' => t('Block display settings'),
'#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
);
// Create the dropdown for choosing the display format.
$form['displays']['block']['options']['style']['style_plugin'] = array(
'#title' => t('Display format'),
'#type' => 'select',
'#options' => $style_options,
);
$style_form = &$form['displays']['block']['options']['style'];
$style_form['style_plugin']['#default_value'] = static::getSelected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
// Changing this dropdown updates $form['displays']['block']['options'] via
// AJAX.
views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
// Create the dropdown for choosing the display format.
$form['displays']['block']['options']['style']['style_plugin'] = array(
'#title' => t('Display format'),
'#type' => 'select',
'#options' => $style_options,
);
$style_form = &$form['displays']['block']['options']['style'];
$style_form['style_plugin']['#default_value'] = static::getSelected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
// Changing this dropdown updates $form['displays']['block']['options']
// via AJAX.
views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
$this->buildFormStyle($form, $form_state, 'block');
$form['displays']['block']['options']['items_per_page'] = array(
'#title' => t('Items per block'),
'#type' => 'number',
'#default_value' => 5,
'#min' => 0,
);
$form['displays']['block']['options']['pager'] = array(
'#title' => t('Use a pager'),
'#type' => 'checkbox',
'#default_value' => FALSE,
);
}
$this->buildFormStyle($form, $form_state, 'block');
$form['displays']['block']['options']['items_per_page'] = array(
'#title' => t('Items per block'),
'#type' => 'number',
'#default_value' => 5,
'#min' => 0,
);
$form['displays']['block']['options']['pager'] = array(
'#title' => t('Use a pager'),
'#type' => 'checkbox',
'#default_value' => FALSE,
);
// Only offer the REST export settings if the module is enabled.
if (\Drupal::moduleHandler()->moduleExists('rest')) {
$form['displays']['rest_export'] = array(
'#type' => 'fieldset',
'#title' => t('REST export settings'),
'#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend')),
'#tree' => TRUE,
);
$form['displays']['rest_export']['create'] = array(
'#title' => t('Provide a REST export'),
'#type' => 'checkbox',
'#attributes' => array('class' => array('strong')),
'#id' => 'edit-rest-export-create',
);
// All options for the REST export display are included in this container
// so they can be hidden as a group when the "Provide a REST export"
// checkbox is unchecked.
$form['displays']['rest_export']['options'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('options-set')),
'#states' => array(
'visible' => array(
':input[name="rest_export[create]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div id="edit-rest-export-wrapper">',
'#suffix' => '</div>',
'#parents' => array('rest_export'),
);
$form['displays']['rest_export']['options']['path'] = array(
'#title' => t('REST export path'),
'#type' => 'textfield',
'#field_prefix' => $path_prefix,
// Account for the leading backslash.
'#maxlength' => 254,
);
}
return $form;
}
......@@ -699,6 +739,11 @@ protected function buildDisplayOptions($form, FormStateInterface $form_state) {
$display_options['block'] = $this->blockDisplayOptions($form, $form_state);
}
// Display: REST export.
if (!empty($form_state['values']['rest_export']['create'])) {
$display_options['rest_export'] = $this->restExportDisplayOptions($form, $form_state);
}
return $display_options;
}
......@@ -755,6 +800,19 @@ protected function addDisplays(View $view, $display_options, $form, FormStateInt
}
}
// Display: REST export.
if (isset($display_options['rest_export'])) {
$display = $executable->newDisplay('rest_export', 'REST export', 'rest_export_1');
// If there is no page or block, the REST export display options should
// become the overall view defaults.
if (!isset($display_options['page']) && !isset($display_options['block'])) {
$this->setDefaultOptions($display_options['rest_export'], $display, $default_display);
}
else {
$this->setOverrideOptions($display_options['rest_export'], $display, $default_display);
}
}
// Initialize displays and merge all plugin default values.
$executable->mergeDefaults();
}
......@@ -1045,6 +1103,25 @@ protected function blockDisplayOptions(array $form, FormStateInterface $form_sta
return $display_options;
}
/**
* Retrieves the REST export display options from the submitted form values.
*
* @param array $form
* The full wizard form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the wizard form.
*
* @return array
* Returns an array of display options.
*/
protected function restExportDisplayOptions(array $form, FormStateInterface $form_state) {
$display_options = array();
$display_options['path'] = $form_state['values']['rest_export']['path'];
$display_options['style'] = array('type' => 'serializer');
return $display_options;
}
/**
* Retrieves the feed display options.
*
......
......@@ -7,6 +7,7 @@
namespace Drupal\views\Tests\Wizard;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\String;
use Drupal\views\Views;
......@@ -40,6 +41,9 @@ function testViewsWizardAndListing() {
$this->assertLinkByHref(url('admin/structure/views/view/' . $view1['id'] . '/delete'));
$this->assertLinkByHref(url('admin/structure/views/view/' . $view1['id'] . '/duplicate'));
// The view should not have a REST export display.
$this->assertNoText('REST export', 'When no options are enabled in the wizard, the resulting view does not have a REST export display.');
// This view should not have a block.
$this->drupalGet('admin/structure/block');
$this->assertNoText($view1['label']);
......@@ -86,6 +90,9 @@ function testViewsWizardAndListing() {
$this->assertText($view2['description']);
$this->assertLinkByHref(url($view2['page[path]']));
// The view should not have a REST export display.
$this->assertNoText('REST export', 'If only the page option was enabled in the wizard, the resulting view does not have a REST export display.');
// This view should not have a block.
$this->drupalGet('admin/structure/block');
$this->assertNoText('View: ' . $view2['label']);
......@@ -118,6 +125,9 @@ function testViewsWizardAndListing() {
$this->assertText($view3['description']);
$this->assertLinkByHref(url($view3['page[path]']));
// The view should not have a REST export display.
$this->assertNoText('REST export', 'If only the page and block options were enabled in the wizard, the resulting view does not have a REST export display.');
// Confirm that the block is available in the block administration UI.
$this->drupalGet('admin/structure/block/list/' . \Drupal::config('system.theme')->get('default'));
$this->assertText($view3['label']);
......@@ -133,6 +143,25 @@ function testViewsWizardAndListing() {
// Make sure the listing page doesn't show disabled default views.
$this->assertNoText('tracker', 'Default tracker view does not show on the listing page.');
// Create a view with only a REST export.
$view4 = array();
$view4['label'] = $this->randomMachineName(16);
$view4['id'] = strtolower($this->randomMachineName(16));
$view4['description'] = $this->randomMachineName(16);
$view4['show[wizard_key]'] = 'node';
$view4['show[type]'] = 'page';
$view4['rest_export[create]'] = 1;
$view4['rest_export[path]'] = $this->randomMachineName(16);
$this->drupalPostForm('admin/structure/views/add', $view4, t('Save and edit'));
// Check that the REST export path works.
$this->drupalGet($view4['rest_export[path]']);
$this->assertResponse(200);
$data = Json::decode($this->content);
$this->assertEqual(count($data), 1, 'Only the node of type page is exported.');
$node = reset($data);
$this->assertEqual($node['nid'][0]['value'], $node1->id(), 'The node of type page is exported.');
}
/**
......
......@@ -19,7 +19,7 @@ abstract class WizardTestBase extends ViewTestBase {
*
* @var array
*/
public static $modules = array('node', 'views_ui', 'block');
public static $modules = array('node', 'views_ui', 'block', 'rest');
function setUp() {
parent::setUp();
......
......@@ -337,7 +337,8 @@ th.views-ui-operations {
}
.form-item-page-create label,
.form-item-block-create label {
.form-item-block-create label,
.form-item-rest-export-create label {
font-weight: bold;
}
......
......@@ -33,6 +33,8 @@ public function testWizardFieldLength() {
$view['page[feed_properties][path]'] = $this->randomMachineName(255);
$view['block[create]'] = TRUE;
$view['block[title]'] = $this->randomMachineName(256);
$view['rest_export[create]'] = TRUE;
$view['rest_export[path]'] = $this->randomMachineName(255);
$this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
......@@ -42,6 +44,7 @@ public function testWizardFieldLength() {
$this->assertText('View name cannot be longer than 255 characters but is currently 256 characters long.');
$this->assertText('Feed path cannot be longer than 254 characters but is currently 255 characters long.');
$this->assertText('Block title cannot be longer than 255 characters but is currently 256 characters long.');
$this->assertText('REST export path cannot be longer than 254 characters but is currently 255 characters long.');
$view['label'] = $this->randomMachineName(255);
$view['id'] = strtolower($this->randomMachineName(128));
......@@ -52,6 +55,8 @@ public function testWizardFieldLength() {
$view['page[feed_properties][path]'] = $this->randomMachineName(254);
$view['block[create]'] = TRUE;
$view['block[title]'] = $this->randomMachineName(255);
$view['rest_export[create]'] = TRUE;
$view['rest_export[path]'] = $this->randomMachineName(254);
$this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
$this->assertUrl('admin/structure/views/view/' . $view['id'], array(), 'Make sure the view saving was successful and the browser got redirected to the edit page.');
......
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