Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A little cleanup to incorporate into the viewfield D8 module. #7

Open
wants to merge 2 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\viewfield\Plugin\Field\FieldFormatter\ViewfieldDefaultFormatter.
*/


namespace Drupal\viewfield\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
Expand Down Expand Up @@ -50,4 +44,5 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
}
return $elements;
}

}
36 changes: 17 additions & 19 deletions src/Plugin/Field/FieldType/ViewfieldItem.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains \Drupal\viewfield\Plugin\Field\FieldType\ViewfieldItem.
*/

namespace Drupal\viewfield\Plugin\Field\FieldType;

use Drupal\Core\TypedData\DataDefinition;
Expand All @@ -13,7 +8,6 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Views;


/**
* Plugin implementation of the 'viewfield' field type.
*
Expand All @@ -27,7 +21,7 @@
*/
class ViewfieldItem extends FieldItemBase {

static $propertyDefinitions;
public static $propertyDefinitions;

/**
* {@inheritdoc}
Expand All @@ -37,7 +31,6 @@ public function isEmpty() {
return empty($value);
}


/**
* {@inheritdoc}
*/
Expand All @@ -48,7 +41,6 @@ public static function defaultFieldSettings() {
) + parent::defaultFieldSettings();
}


/**
* {@inheritdoc}
*/
Expand All @@ -58,14 +50,16 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
'vname' => array(
'type' => 'varchar',
'not null' => FALSE,
// Views requires at least 96 chars for the view name and display, plus
// we need 1 for our delimiter. Round up to a common value of 128.
// Views requires at least 96 chars for the view name and display,
// plus we need 1 for our delimiter. Round up to a common value of
// 128.
'length' => 128,
),
'vargs' => array(
'type' => 'varchar',
'not null' => FALSE,
'length' => 255, //viewfield_field_instance_settings_form_validate
// Note: viewfield_field_instance_settings_form_validate.
'length' => 255,
),
),
);
Expand All @@ -82,9 +76,9 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
return $properties;
}

/**
* {@inheritdoc}
*/
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form = array(
'#element_validate' => array(array(get_class($this), 'fieldSettingsFormValidate')),
Expand Down Expand Up @@ -115,16 +109,20 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
$force_default = $form_state->getValue(array('settings', 'force_default'));
if ($force_default) {
/**
* @var \Drupal\Core\Field\FieldConfigBase
*/
// The $field is of type \Drupal\Core\Field\FieldConfigBase.
$field = $form_state->getFormObject()->getEntity();
$default_value_vname = $form_state->getValue(array('default_value_input', $field->getName(), 0, 'vname'));
$default_value_vname = $form_state->getValue(array(
'default_value_input',
$field->getName(),
0,
'vname',
));
if (empty($default_value_vname)) {
$form_state->setErrorByName('default_value_input', t('%title requires a default value.', array(
'%title' => $form['force_default']['#title'],
)));
}
}
}

}
19 changes: 6 additions & 13 deletions src/Plugin/Field/FieldWidget/ViewfieldWidget.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

/**
* @file
* Contains \Drupal\viewfield\Plugin\Field\FieldWidget\ViewfieldWidget.
*/

namespace Drupal\viewfield\Plugin\Field\FieldWidget;

use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\views\Views;

/**
Expand All @@ -26,7 +20,6 @@
*/
class ViewfieldWidget extends WidgetBase {


/**
* {@inheritdoc}
*/
Expand All @@ -48,17 +41,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#title' => t('Arguments'),
'#default_value' => isset($items[$delta]->vargs) ? $items[$delta]->vargs : NULL,
'#access' => !$field_settings['force_default'],
'#description' => t('A comma separated list of arguments to pass to the selected view. '),
'#description' => t('A comma separated list of arguments to pass to the selected view.'),
);

return $element;
}

/**
* Returns a select options list of views displays of enabled and allowed views.
* Returns a select options list of enabled and allowed views displays.
*
* @param array @settings
* The field settings
* @param array $settings
* The field settings.
*
* @return array
* An array with the allowed and enabled views and displays.
Expand All @@ -68,8 +61,8 @@ protected function getPotentialReferences($settings) {
$views = Views::getEnabledViews();
// Limit to allowed values, if any.
if (isset($settings['allowed_views']) && is_array($settings['allowed_views'])) {
// Only intersect if at least one view has been enabled; otherwise, we would
// end up with empty $views.
// Only intersect if at least one view has been enabled; otherwise, we
// would end up with empty $views.
if ($allowed = array_filter($settings['allowed_views'])) {
$views = array_intersect_key($views, $allowed);
}
Expand Down
65 changes: 36 additions & 29 deletions src/Tests/ViewFieldTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains Drupal\viewfield\Tests\ViewFieldTest.
*/

namespace Drupal\viewfield\Tests;

use Drupal\Component\Utility\Unicode;
Expand Down Expand Up @@ -38,6 +33,9 @@ class ViewFieldTest extends WebTestBase {
*/
protected $field;

/**
* Set up the test.
*/
protected function setUp() {
parent::setUp();

Expand All @@ -52,7 +50,7 @@ protected function setUp() {
/**
* Test field creation and attachment to an article.
*/
function testFieldCreation() {
public function testFieldCreation() {
$field_name = Unicode::strtolower($this->randomMachineName());
// Create a field with settings to validate.
$this->fieldStorage = entity_create('field_storage_config', array(
Expand Down Expand Up @@ -87,19 +85,25 @@ function testFieldCreation() {
$this->assertViewDisplays($field_name);
}

/**
* Assert that the node was created.
*
* @param string $field_name
* A name of the field.
*/
protected function assertNodeCreated($field_name) {
// Display article creation form.
$this->drupalGet('node/add/article');
$view_select_name = "{$field_name}[0][vname]";
$this->assertFieldByName($view_select_name, NULL,'Views select list is displayed.');
$this->assertFieldByName("{$field_name}[0][vargs]", '' ,
$this->assertFieldByName($view_select_name, NULL, 'Views select list is displayed.');
$this->assertFieldByName("{$field_name}[0][vargs]", '',
'Views arguments text field is displayed');

$edit = array (
$edit = array(
"title[0][value]" => 'Test',
$view_select_name => 'user_admin_people|default',
);
// create article with viewfield
// Create article with viewfield.
$this->drupalPostForm(NULL, $edit, t('Save and publish'));
$this->assertText(t('Article Test has been created.'));
}
Expand All @@ -108,37 +112,40 @@ protected function assertNodeCreated($field_name) {
* Assert that the view is displayed on a node.
*
* @param string $field_name
* The field to test
* The field to test.
*/
protected function assertViewDisplays($field_name) {
// create article
// Create article.
$this->drupalGet('node/add/article');
$view_select_name = "{$field_name}[0][vname]";
$edit = array (
$edit = array(
"title[0][value]" => 'Test1',
$view_select_name => 'user_admin_people|default',
);
$this->drupalPostForm(NULL, $edit, t('Save and publish'));
// test that the view displays on the node

// Test that the view displays on the node.
$elements = $this->xpath("//div[contains(@class,:class) and contains(@class,:class1)]",
array(':class' => 'view-user-admin-people',':class1' => 'view-display-id-default'));
array(':class' => 'view-user-admin-people', ':class1' => 'view-display-id-default'));
$this->assertTrue(!empty($elements), 'Node contains the correct view and display.');
$elements = $this->xpath("//a[@href=:href]",array(':href' => '/user/1'));
$elements = $this->xpath("//a[@href=:href]", array(':href' => '/user/1'));
$this->assertTrue(!empty($elements), 'View is displaying the content.');
}

/**
* Assert that a default view is required when default value checkbox is checked.
* Assert for a default view when default value checkbox is checked.
*
* Assert that a default view is required when default value checkbox is
* checked.
*
* @param string $field_name
* The field to test
* The field to test.
*/
protected function assertDefaultViewRequired($field_name) {
$this->drupalGet("admin/structure/types/manage/article/fields/node.article.{$field_name}");
$default_chk_name = 'field[settings][force_default]';
$this->assertFieldByName($default_chk_name, NULL,'Default value checkbox displayed');
$edit = array (
$this->assertFieldByName($default_chk_name, NULL, 'Default value checkbox displayed');
$edit = array(
$default_chk_name => TRUE,
);
$this->drupalPostForm(NULL, $edit, t('Save settings'));
Expand All @@ -149,30 +156,30 @@ protected function assertDefaultViewRequired($field_name) {
* Assert that the default view is selected on the node add form.
*
* @param string $field_name
* The field to test
* The field to test.
*/
protected function assertDefaultViewSelected($field_name) {
$this->drupalGet("admin/structure/types/manage/article/fields/node.article.{$field_name}");
$default_view_select_name = "default_value_input[{$field_name}][0][vname]";
$this->assertFieldByName($default_view_select_name, NULL,'Default view select list is displayed');
$edit = array (
$this->assertFieldByName($default_view_select_name, NULL, 'Default view select list is displayed');
$edit = array(
$default_view_select_name => 'user_admin_people|default',
);
$this->drupalPostForm(NULL, $edit, t('Save settings'));
$this->assertText("Saved {$field_name} configuration");

// check that the view is preselected on the node form
// Check that the view is preselected on the node form.
$this->drupalGet('node/add/article');
$view_select_name = "{$field_name}[0][vname]";

$this->assertFieldByName($view_select_name, 'user_admin_people|default','Views select list is displayed with correct value');
$this->assertFieldByName($view_select_name, 'user_admin_people|default', 'Views select list is displayed with correct value');

// return the default value to its original state
// Return the default value to its original state.
$this->drupalGet("admin/structure/types/manage/article/fields/node.article.{$field_name}");
$edit = array (
$edit = array(
$default_view_select_name => '0',
);
$this->drupalPostForm(NULL, $edit, t('Save settings'));

}

}
Loading