diff --git a/content_entity_example/src/ContactAccessControlHandler.php b/content_entity_example/src/ContactAccessControlHandler.php new file mode 100755 index 0000000..5b812a9 --- /dev/null +++ b/content_entity_example/src/ContactAccessControlHandler.php @@ -0,0 +1,56 @@ +hasPermission('view contact entity'); + break; + + case 'edit': + return $account->hasPermission('edit contact entity'); + break; + + case 'delete': + return $account->hasPermission('delete contact entity'); + break; + + } + + return TRUE; + } + + /** + * {@inheritdoc} + * + * Separate from the checkAccess because the entity does not yet exist, it + * will be created during the 'add' process. + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return $account->hasPermission('add contact entity'); + } +} diff --git a/content_entity_example/src/ContactAccessController.php b/content_entity_example/src/ContactAccessController.php deleted file mode 100755 index 1b2c4ee..0000000 --- a/content_entity_example/src/ContactAccessController.php +++ /dev/null @@ -1,56 +0,0 @@ -hasPermission('view contact entity'); - break; - - case 'edit': - return $account->hasPermission('edit contact entity'); - break; - - case 'delete': - return $account->hasPermission('delete contact entity'); - break; - - } - - return TRUE; - } - - /** - * {@inheritdoc} - * - * Separate from the checkAccess because the entity does not yet exist, it - * will be created during the 'add' process. - */ - protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { - return $account->hasPermission('add contact entity'); - } -} diff --git a/content_entity_example/src/Entity/Contact.php b/content_entity_example/src/Entity/Contact.php index 709cb52..e4e8514 100755 --- a/content_entity_example/src/Entity/Contact.php +++ b/content_entity_example/src/Entity/Contact.php @@ -18,71 +18,70 @@ use Drupal\user\UserInterface; * @ingroup content_entity_example * * This is the main definition of the entity type. From it, an entityType is - * derived. The most important properties in this example are: + * derived. The most important properties in this example are listed below. * - * - id: The unique identifier of this entityType. It follows the - * pattern 'moduleName_xyz' to avoid naming conflicts. + * id: The unique identifier of this entityType. It follows the pattern + * 'moduleName_xyz' to avoid naming conflicts. * - * - label: Human readable name of the entity type. + * label: Human readable name of the entity type. * - * - controllers: Controller classes are used for different tasks. You can use - * standard controllers provided by D8 or build your own - * controller, most probably derived from the standard class. + * controllers: Controller classes are used for different tasks. You can use + * standard controllers provided by D8 or build your own controller, most + * probably derived from the standard class. In detail: * - * view_builder: we use the standard controller to view an - * instance. It is called when a route lists an - * '_entity_view' default for the entityType - * (see routing.yml for details. The view can be - * manipulated by using the standard drupal tools - * in the settings. - * list builder: We derive out own list builder class from the - * entityListBuilder to control the presentation. - * If there is a view available for this entity - * from the views module, it overrides the list - * builder. @todo: any view? naming convention? - * form: We derive our own forms to add functionality - * like additional fields, redirects etc. - * These forms are called when the routing list - * an '_entity_form' default for the entityType. - * Depending on the suffix (.add/.edit/.delete) - * in the route, the correct form is called. - * access: Our own accessController where we determine - * access rights based on permissions. + * - view_builder: we use the standard controller to view an instance. It is + * called when a route lists an '_entity_view' default for the entityType + * (see routing.yml for details. The view can be manipulated by using the + * standard drupal tools in the settings. * - * - base_table: Define the name of the table used to store the data. Make - * sure it is unique. The schema is automatically determined - * from the BaseFieldDefinitions below. The table is - * automatically created during installation. + * - list-builder: We derive our own list builder class from the + * entityListBuilder to control the presentation. + * If there is a view available for this entity from the views module, it + * overrides the list builder. @todo: any view? naming convention? * - * - fieldable: Can additional fields be added to the entity via the GUI? - * Analog to content types. + * - form: We derive our own forms to add functionality like additional fields, + * redirects etc. These forms are called when the routing list an + * '_entity_form' default for the entityType. Depending on the suffix + * (.add/.edit/.delete) in the route, the correct form is called. * - * - entity_keys:How to access the fields. Analog to 'nid' or 'uid'. + * - access: Our own accessController where we determine access rights based on + * permissions. * - * - links: Provide links to do standard tasks. The 'edit-form' and - * 'delete-form' links are added to the list built by the - * entityListController. They will show up as action buttons in - * an additional column. @todo: understand and explain the admin - * link thing + * - base_table: Define the name of the table used to store the data. Make sure + * it is unique. The schema is automatically determined from the + * BaseFieldDefinitions below. The table is automatically created during + * installation. + * + * - fieldable: Can additional fields be added to the entity via the GUI? + * Analog to content types. + * + * - entity_keys: How to access the fields. Analog to 'nid' or 'uid'. + * + * - links: Provide links to do standard tasks. The 'edit-form' and + * 'delete-form' links are added to the list built by the + * entityListController. They will show up as action buttons in an additional + * column. @todo: understand and explain the admin-link thing * * There are many more properties to be used in an entity type definition. For * a complete overview, please refer to the '\Drupal\Core\Entity\EntityType' * class definition. * + * The following construct is the actual definition of the entity type which + * ist read and cached. Don't forget to clear cache after changes. * * @ContentEntityType( * id = "content_entity_example_contact", * label = @Translation("Contact entity"), * controllers = { * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", - * "list_builder" = "Drupal\content_entity_example\Entity\Controller\ContactListController", + * "list_builder" = "Drupal\content_entity_example\Entity\Controller\ContactListBuilder", * * "form" = { * "add" = "Drupal\content_entity_example\Form\ContactForm", * "edit" = "Drupal\content_entity_example\Form\ContactForm", * "delete" = "Drupal\content_entity_example\Form\ContactDeleteForm", * }, - * "access" = "Drupal\content_entity_example\ContactAccessController", + * "access" = "Drupal\content_entity_example\ContactAccessControlHandler", * }, * base_table = "contact", * admin_permission = "administer content_entity_example entity", @@ -120,8 +119,8 @@ class Contact extends ContentEntityBase implements ContactInterface { /** * {@inheritdoc} * - * When a new entity instance is added, make sure that the user_id entity - * reference points to the current user as the creator of the instance. + * When a new entity instance is added, set the user_id entity reference to + * the current user as the creator of the instance. */ public static function preCreate(EntityStorageInterface $storage_controller, array &$values) { parent::preCreate($storage_controller, $values); @@ -199,7 +198,7 @@ class Contact extends ContentEntityBase implements ContactInterface { ->setReadOnly(TRUE); // Name field for the contact. - // We set display options for the view as well as the form. + // We set display options for the view as well as the form. // Users with correct privileges can change the view and edit configuration. $fields['name'] = FieldDefinition::create('string') @@ -271,7 +270,7 @@ class Contact extends ContentEntityBase implements ContactInterface { // Owner field of the contact. // Entity reference field, holds the reference to the user object. // The view shows the user name field of the user. - // The form presents a autocomplete field for the user name. + // The form presents a auto complete field for the user name. $fields['user_id'] = FieldDefinition::create('entity_reference') ->setLabel(t('User Name')) ->setDescription(t('The Name of the associated user.')) diff --git a/content_entity_example/src/Entity/Controller/ContactListBuilder.php b/content_entity_example/src/Entity/Controller/ContactListBuilder.php new file mode 100755 index 0000000..f27ddcb --- /dev/null +++ b/content_entity_example/src/Entity/Controller/ContactListBuilder.php @@ -0,0 +1,50 @@ +id(); + $row['name'] = \Drupal::l($this->getLabel($entity), + 'content_entity_example.contact_view', array( + 'content_entity_example_contact' => $entity->id(), + )); + $row['first_name'] = $entity->first_name->value; + $row['gender'] = $entity->gender->value; + return $row + parent::buildRow($entity); + } +} diff --git a/content_entity_example/src/Entity/Controller/ContactListController.php b/content_entity_example/src/Entity/Controller/ContactListController.php deleted file mode 100755 index c4c9f30..0000000 --- a/content_entity_example/src/Entity/Controller/ContactListController.php +++ /dev/null @@ -1,50 +0,0 @@ -id(); - $row['name'] = \Drupal::l($this->getLabel($entity), - 'content_entity_example.contact_view', array( - 'content_entity_example_contact' => $entity->id(), - )); - $row['first_name'] = $entity->first_name->value; - $row['gender'] = $entity->gender->value; - return $row + parent::buildRow($entity); - } -}