diff --git a/field_permission_example/css/field-permission-description.css b/field_permission_example/css/field-permission-description.css index e03060b..a366cc5 100644 --- a/field_permission_example/css/field-permission-description.css +++ b/field_permission_example/css/field-permission-description.css @@ -1,4 +1,3 @@ /** * Field Permssions Example CSS */ - diff --git a/field_permission_example/css/field_permission_example.css b/field_permission_example/css/field_permission_example.css index 59cda31..bf5a999 100644 --- a/field_permission_example/css/field_permission_example.css +++ b/field_permission_example/css/field_permission_example.css @@ -3,20 +3,18 @@ * CSS for Field Example. */ .stickynote { -background:#fefabc; -padding:0.8em; -font-family:cursive; -font-size:1.1em; -color: #000; -width:15em; - --moz-transform: rotate(2deg); --webkit-transform: rotate(2deg); --o-transform: rotate(2deg); --ms-transform: rotate(2deg); -transform: rotate(2deg); - -box-shadow: 0px 4px 6px #333; --moz-box-shadow: 0px 4px 6px #333; --webkit-box-shadow: 0px 4px 6px #333; + background: #fefabc; + padding: 0.8em; + font-family: cursive; + font-size: 1.1em; + color: 1000; + width: 15em; + -moz-transform: rotate(2deg); + -webkit-transform: rotate(2deg); + -o-transform: rotate(2deg); + -ms-transform: rotate(2deg); + transform: rotate(2deg); + box-shadow: 0px 4px 6px #333; + -moz-box-shadow: 0px 4px 6px #333; + -webkit-box-shadow: 0px 4px 6px #333; } diff --git a/field_permission_example/field_permission_example.module b/field_permission_example/field_permission_example.module index ab35d92..62c35ad 100644 --- a/field_permission_example/field_permission_example.module +++ b/field_permission_example/field_permission_example.module @@ -4,7 +4,7 @@ * @file * An example field using the Field Types API. */ - + /** * @defgroup field_permission_example Example: Field Permissions * @ingroup examples @@ -79,31 +79,33 @@ * @see field_types * @see field */ - -//use statements to support hook_entity_field_access + +// Use statements to support hook_entity_field_access. use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Access\AccessResult; -//Interfaces used by entities to declare "ownership" +// Interfaces used by entities to declare "ownership". use Drupal\user\EntityOwnerInterface; use Drupal\user\UserInterface; - + +// Use statements for hook_entity_test_access. +use Drupal\Core\Entity\EntityInterface; + /** * Implements hook_entity_field_access(). * * We want to make sure that fields aren't being seen or edited * by those who shouldn't. - * */ function field_permission_example_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { - //Find out what field we're looking at. If it isn't - //our sticky note widget, tell Drupal we don't care about its access + // Find out what field we're looking at. If it isn't + // our sticky note widget, tell Drupal we don't care about its access. if ($field_definition->getType() != 'field_permission_example_fieldnote') { - return AccessResult::neutral(); + return AccessResult::neutral(); } - + // First we'll check if the user has the 'superuser' // permissions that node provides. This way administrators // will be able to administer the content types. @@ -120,20 +122,20 @@ function field_permission_example_entity_field_access($operation, FieldDefinitio return AccessResult::allowed(); } - //for anyone else, it depends on the desired operation + // For anyone else, it depends on the desired operation. if ($operation == 'view' and $account->hasPermission('view any fieldnote')) { drupal_set_message(t('User can view any field note.')); return AccessResult::allowed(); } - + if ($operation == 'edit' and $account->hasPermission('edit any fieldnote')) { drupal_set_message(t('User can edit any field note.')); return AccessResult::allowed(); } - - //At this point, we need to know if the user "owns" the entity we're attached to. - //If it's a user, we'll use the account name to test. Otherwise rely on the entity implementing the - //the EntityOwnerInterface. Anything else can't be owned, and we'll refuse access. + + // At this point, we need to know if the user "owns" the entity we're attached to. + // If it's a user, we'll use the account name to test. Otherwise rely on the entity implementing the + // the EntityOwnerInterface. Anything else can't be owned, and we'll refuse access. if ($items) { $entity = $items->getEntity(); if ((($entity instanceof EntityOwnerInterface) and @@ -151,24 +153,24 @@ function field_permission_example_entity_field_access($operation, FieldDefinitio } } } - //anything else on this field is forbidden + // Anything else on this field is forbidden. return AccessResult::forbidden(); } /** - * Implementation of hook_ENTITY_TYPE_access + * Implements hook_ENTITY_TYPE_access(). * * Note: this routine is added so we can more easily test our access code. Core * defines an entity_test entity that is used for testing fields in core. We add * this routine to make the entity_test entity editable by our tests. */ -function field_permission_example_entity_test_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account, $langcode) { +function field_permission_example_entity_test_access(EntityInterface $entity, $operation, AccountInterface $account, $langcode) { if ($operation == 'edit') { $perms = [ 'administer the fieldnote field', 'edit any fieldnote', - 'edit own fieldnote' + 'edit own fieldnote', ]; foreach ($perms as $perm) { if ($account->hasPermission($perm)) { @@ -181,19 +183,19 @@ function field_permission_example_entity_test_access(\Drupal\Core\Entity\EntityI /** * @} End of "defgroup field_permission_example". */ - + /** - * Implementation of hook_theme + * Implements hook_theme(). * * Since we have a lot to explain, we're going to use Twig to do it. */ function field_permission_example_theme() { - return [ - 'field_permission_description' => [ - 'template' => 'description', - 'variables' => [ - 'admin_link' => NULL - ] - ] - ]; + return [ + 'field_permission_description' => [ + 'template' => 'description', + 'variables' => [ + 'admin_link' => NULL, + ], + ], + ]; } diff --git a/field_permission_example/src/Controller/FieldPermissionExampleController.php b/field_permission_example/src/Controller/FieldPermissionExampleController.php index 5622d0d..be6a2c5 100644 --- a/field_permission_example/src/Controller/FieldPermissionExampleController.php +++ b/field_permission_example/src/Controller/FieldPermissionExampleController.php @@ -22,12 +22,12 @@ class FieldPermissionExampleController extends ControllerBase { // Make a link from a route to the permissions admin page. $url = Url::fromRoute('user.admin_permissions'); $permissions_admin_link = $this->l($this->t('the permissions admin page'), $url); - + $build = [ 'description' => [ '#theme' => 'field_permission_description', '#admin_link' => $permissions_admin_link, - ] + ], ]; return $build; } diff --git a/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php b/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php index ac9229c..3e737d8 100644 --- a/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php +++ b/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php @@ -28,14 +28,14 @@ class SimpleTextFormatter extends FormatterBase { * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { - $elements = array(); + $elements = array(); foreach ($items as $delta => $item) { $elements[$delta] = array( // We wrap the fieldnote content up in a div tag. '#type' => 'html_tag', '#tag' => 'div', - //this text is auto-XSS escaped. See docs for @RenderElement("html_tag") + // This text is auto-XSS escaped. See docs for @RenderElement("html_tag"). '#value' => $item->value, // Let's give the note a nice sticky-note CSS appearance. '#attributes' => array( @@ -47,7 +47,7 @@ class SimpleTextFormatter extends FormatterBase { ), ); } - + return $elements; } diff --git a/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php b/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php index bdccde4..c34ef68 100644 --- a/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php +++ b/field_permission_example/src/Plugin/Field/FieldType/FieldNote.php @@ -8,7 +8,6 @@ namespace Drupal\field_permission_example\Plugin\Field\FieldType; use Drupal\Core\Field\FieldItemBase; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; @@ -57,4 +56,5 @@ class FieldNote extends FieldItemBase { return $properties; } + } diff --git a/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php b/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php index 1cf08db..e4a02f9 100644 --- a/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php +++ b/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php @@ -31,8 +31,8 @@ class TextWidget extends WidgetBase { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $value = isset($items[$delta]->value) ? $items[$delta]->value : ''; $element += array( - '#type' => 'textarea', - '#default_value' => $value, + '#type' => 'textarea', + '#default_value' => $value, ); return array('value' => $element); } diff --git a/field_permission_example/src/Tests/FieldNoteItemTest.php b/field_permission_example/src/Tests/FieldNoteItemTest.php index 8c79669..b4e0977 100644 --- a/field_permission_example/src/Tests/FieldNoteItemTest.php +++ b/field_permission_example/src/Tests/FieldNoteItemTest.php @@ -13,11 +13,8 @@ namespace Drupal\field_permission_example\Tests; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; -use Drupal\field\Entity\FieldStorageConfig; -use Drupal\field\Entity\FieldConfig; use Drupal\field\Tests\FieldUnitTestBase; use Drupal\simpletest\UserCreationTrait; -use Drupal\Core\Access\AccessResult; /** * Tests the new entity API for the email field type. @@ -25,14 +22,19 @@ use Drupal\Core\Access\AccessResult; * @group field */ class FieldNoteItemTest extends FieldUnitTestBase { - + use UserCreationTrait; + /** + * {@inheritdoc} + * + * This sets up the entity_test and user types to use our example field plugins. + */ protected function setUp() { array_unshift(self::$modules, 'field_permission_example'); parent::setUp(); - //set up our entity_type and user type for our new field: + // Set up our entity_type and user type for our new field: entity_create('field_storage_config', array( 'field_name' => 'field_fieldnote', 'entity_type' => 'entity_test', @@ -50,8 +52,8 @@ class FieldNoteItemTest extends FieldUnitTestBase { 'type' => 'field_permission_example_widget', )) ->save(); - - //Now do this for the user type + + // Now do this for the user type. entity_create('field_storage_config', array( 'field_name' => 'user_fieldnote', 'entity_type' => 'user', @@ -68,7 +70,7 @@ class FieldNoteItemTest extends FieldUnitTestBase { ->setComponent('user_fieldnote', array( 'type' => 'field_permission_example_widget', )) - ->save(); + ->save(); } /** @@ -110,8 +112,8 @@ class FieldNoteItemTest extends FieldUnitTestBase { * Test multiple access scenarios for the fieldnote field. */ public function testFieldNoteAccess() { - - //Let's set up some scenarios + + // Let's set up some scenarios. $scenarios = [ 'admin_type' => [ 'perms' => ['administer the fieldnote field'], @@ -128,28 +130,42 @@ class FieldNoteItemTest extends FieldUnitTestBase { 'can_edit_own' => FALSE, ], 'view_any' => [ - 'perms' => ['view test entity', 'view any fieldnote'], + 'perms' => [ + 'view test entity', + 'view any fieldnote', + ], 'can_view_any' => TRUE, 'can_edit_any' => FALSE, 'can_view_own' => FALSE, 'can_edit_own' => FALSE, ], 'edit_any' => [ - 'perms' => ['view test entity', 'view any fieldnote', 'edit any fieldnote'], + 'perms' => [ + 'view test entity', + 'view any fieldnote', + 'edit any fieldnote', + ], 'can_view_any' => TRUE, 'can_edit_any' => TRUE, 'can_view_own' => FALSE, 'can_edit_own' => FALSE, ], 'view_own' => [ - 'perms' => ['view test entity', 'view own fieldnote'], + 'perms' => [ + 'view test entity', + 'view own fieldnote', + ], 'can_view_any' => FALSE, 'can_edit_any' => FALSE, 'can_view_own' => TRUE, 'can_edit_own' => FALSE, ], 'edit_own' => [ - 'perms' => ['view test entity', 'view own fieldnote', 'edit own fieldnote'], + 'perms' => [ + 'view test entity', + 'view own fieldnote', + 'edit own fieldnote', + ], 'can_view_any' => FALSE, 'can_edit_any' => FALSE, 'can_view_own' => TRUE, @@ -158,12 +174,12 @@ class FieldNoteItemTest extends FieldUnitTestBase { ]; $value = 'This is an epic entity'; - //we also need to test users as an entity to attach to. They work - //a little differently than most content entity types: + // We also need to test users as an entity to attach to. They work + // a little differently than most content entity types: $arbitrary_user = $this->createUser([], 'Some User'); $arbitrary_user->user_fieldnote = $value; $arbitrary_user->save(); - + foreach ($scenarios as $name => $scenario) { $test_user = $this->createUser($scenario['perms'], $name); $entity = entity_create('entity_test'); @@ -171,18 +187,17 @@ class FieldNoteItemTest extends FieldUnitTestBase { $entity->name->value = $this->randomMachineName(); $entity->save(); - foreach (['can_view_any', 'can_edit_any'] as $op) { $this->doAccessAssertion($entity, 'field_fieldnote', $test_user, $name, $op, $scenario[$op]); $this->doAccessAssertion($arbitrary_user, 'user_fieldnote', $test_user, $name, $op, $scenario[$op]); } - + if ($scenario['can_view_own'] or $scenario['can_edit_own']) { $entity->user_id = $test_user; $entity->save(); $test_user->user_fieldnote = $value; $test_user->save(); - + foreach (['can_view_own', 'can_edit_own'] as $op) { $this->doAccessAssertion($entity, 'field_fieldnote', $test_user, $name, $op, $scenario[$op]); $this->doAccessAssertion($test_user, 'user_fieldnote', $test_user, $name, $op, $scenario[$op]); @@ -191,9 +206,9 @@ class FieldNoteItemTest extends FieldUnitTestBase { } } - + /** - * helper routine to run the assertions. + * Helper routine to run the assertions. */ protected function doAccessAssertion($entity, $field_name, $account, $name, $op, $expected) { $expect_str = $expected ? "CAN" : "CANNOT"; @@ -204,7 +219,7 @@ class FieldNoteItemTest extends FieldUnitTestBase { $this->assertTrue($result, $assert_str); } else { - $this->assertFalse($result, $assert_str); + $this->assertFalse($result, $assert_str); } }