diff --git a/content_entity_example/content_entity_example.info.yml b/content_entity_example/content_entity_example.info.yml index d9a98fe..d06c8d0 100755 --- a/content_entity_example/content_entity_example.info.yml +++ b/content_entity_example/content_entity_example.info.yml @@ -3,3 +3,6 @@ type: module description: 'Provides ContentEntityExampleContact entity.' package: Example modules core: 8.x +dependencies: + - options + - entity_reference diff --git a/content_entity_example/content_entity_example.links.action.yml b/content_entity_example/content_entity_example.links.action.yml new file mode 100755 index 0000000..f843b9c --- /dev/null +++ b/content_entity_example/content_entity_example.links.action.yml @@ -0,0 +1,6 @@ +content_entity_example.contact_add: + route_name: content_entity_example.contact_add + title: 'Add Contact' + appears_on: + - content_entity_example.contact_list + - content_entity_example.contact_view diff --git a/content_entity_example/content_entity_example.links.menu.yml b/content_entity_example/content_entity_example.links.menu.yml new file mode 100755 index 0000000..d732d65 --- /dev/null +++ b/content_entity_example/content_entity_example.links.menu.yml @@ -0,0 +1,10 @@ +content_entity_example.contact_list: + title: 'Content Entity Example: Contacts Listing' + route_name: content_entity_example.contact_list + description: 'List Contacts' + weight: 10 +content_entity_example_contact.admin.structure.settings: + title: Contact Settings + description: 'Configure Contact entity' + route_name: content_entity_example.contact_settings + parent: system.admin_structure diff --git a/content_entity_example/content_entity_example.links.task.yml b/content_entity_example/content_entity_example.links.task.yml new file mode 100755 index 0000000..d18022e --- /dev/null +++ b/content_entity_example/content_entity_example.links.task.yml @@ -0,0 +1,20 @@ +contact.settings_tab: + route_name: content_entity_example.contact_settings + title: 'Settings' + base_route: content_entity_example.contact_settings + +content_entity_example.contact_view: + route_name: content_entity_example.contact_view + base_route: content_entity_example.contact_view + title: 'View' + +contact.page_edit: + route_name: content_entity_example.contact_edit + base_route: content_entity_example.contact_view + title: Edit + +contact.delete_confirm: + route_name: content_entity_example.contact_delete + base_route: content_entity_example.contact_view + title: Delete + weight: 10 diff --git a/content_entity_example/content_entity_example.local_actions.yml b/content_entity_example/content_entity_example.local_actions.yml deleted file mode 100755 index f843b9c..0000000 --- a/content_entity_example/content_entity_example.local_actions.yml +++ /dev/null @@ -1,6 +0,0 @@ -content_entity_example.contact_add: - route_name: content_entity_example.contact_add - title: 'Add Contact' - appears_on: - - content_entity_example.contact_list - - content_entity_example.contact_view diff --git a/content_entity_example/content_entity_example.local_tasks.yml b/content_entity_example/content_entity_example.local_tasks.yml deleted file mode 100755 index d18022e..0000000 --- a/content_entity_example/content_entity_example.local_tasks.yml +++ /dev/null @@ -1,20 +0,0 @@ -contact.settings_tab: - route_name: content_entity_example.contact_settings - title: 'Settings' - base_route: content_entity_example.contact_settings - -content_entity_example.contact_view: - route_name: content_entity_example.contact_view - base_route: content_entity_example.contact_view - title: 'View' - -contact.page_edit: - route_name: content_entity_example.contact_edit - base_route: content_entity_example.contact_view - title: Edit - -contact.delete_confirm: - route_name: content_entity_example.contact_delete - base_route: content_entity_example.contact_view - title: Delete - weight: 10 diff --git a/content_entity_example/content_entity_example.menu_links.yml b/content_entity_example/content_entity_example.menu_links.yml deleted file mode 100755 index d732d65..0000000 --- a/content_entity_example/content_entity_example.menu_links.yml +++ /dev/null @@ -1,10 +0,0 @@ -content_entity_example.contact_list: - title: 'Content Entity Example: Contacts Listing' - route_name: content_entity_example.contact_list - description: 'List Contacts' - weight: 10 -content_entity_example_contact.admin.structure.settings: - title: Contact Settings - description: 'Configure Contact entity' - route_name: content_entity_example.contact_settings - parent: system.admin_structure diff --git a/content_entity_example/content_entity_example.module b/content_entity_example/content_entity_example.module index f4b97e5..0fb32d0 100755 --- a/content_entity_example/content_entity_example.module +++ b/content_entity_example/content_entity_example.module @@ -11,7 +11,15 @@ use Symfony\Component\HttpFoundation\Request; * @defgroup content_entity_example Example: Content Entity * @ingroup examples * @{ - * Example of how to define a content entity. + * Implement a Content entity. + * + * This module demonstrates implementing a Content Entity. + * + * This is an example of a fieldable content entity used to hold structured information + * without the overhead of using a content type. It is defined programmatically and we will expose + * the main techniques to handle and expose the contents. + * + * We define a content entity named 'Contact'. * } */ @@ -41,14 +49,14 @@ function content_entity_example_permission() { /** * Implements hook_help(). */ -function content_entity_example_help($route_name, Request $request) { +function content_entity_example_help($route_name) { switch ($route_name) { // Main module help for the block module. case 'content_entity_example.contact_list': return '
' . - t('Content Entity Example implements a Contacts model. These contacts are fieldable entities. You can manage the fields on the Contacts admin page.', array( + t('Content Entity Example implements a Contacts model. These contacts are fieldable entities. You can manage the fields on the Contacts admin page.', array( '@adminlink' => \Drupal::urlGenerator()->generateFromRoute('content_entity_example.contact_settings'), - ) - ) . '
'; + ) + ) . ''; } -} +} \ No newline at end of file diff --git a/content_entity_example/src/Entity/Contact.php b/content_entity_example/src/Entity/Contact.php index 824dc21..8128d48 100755 --- a/content_entity_example/src/Entity/Contact.php +++ b/content_entity_example/src/Entity/Contact.php @@ -156,13 +156,14 @@ class Contact extends ContentEntityBase implements ContactInterface { ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); - $fields['gender'] = FieldDefinition::create('string') + $fields['gender'] = FieldDefinition::create('list_text') ->setLabel(t('Gender')) - ->setDescription(t('The gender of the ContentEntityExample entity.')) + ->setDescription(t('The gender of the Contact entity.')) ->setSettings(array( - 'default_value' => '', - 'max_length' => 255, - 'text_processing' => 0, + 'allowed_values' => array( + 'female' => 'female', + 'male' => 'male', + ), )) ->setDisplayOptions('view', array( 'label' => 'above', diff --git a/content_entity_example/src/Form/ContactDeleteForm.php b/content_entity_example/src/Form/ContactDeleteForm.php index 55e31e0..b555b4d 100755 --- a/content_entity_example/src/Form/ContactDeleteForm.php +++ b/content_entity_example/src/Form/ContactDeleteForm.php @@ -8,6 +8,7 @@ namespace Drupal\content_entity_example\Form; use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; /** @@ -28,7 +29,7 @@ class ContactDeleteForm extends ContentEntityConfirmFormBase { /** * {@inheritdoc} */ - public function getCancelRoute() { + public function getCancelURL() { return new Url('content_entity_example.contact_list'); } @@ -42,11 +43,11 @@ class ContactDeleteForm extends ContentEntityConfirmFormBase { /** * {@inheritdoc} */ - public function submit(array $form, array &$form_state) { + public function submit(array $form, FormStateInterface $form_state) { $this->entity->delete(); watchdog('content', '@type: deleted %title.', array('@type' => $this->entity->bundle(), '%title' => $this->entity->label())); - $form_state['redirect_route']['route_name'] = 'content_entity_example.contact_list'; + $form_state->setRedirect('content_entity_example.contact_list'); } } diff --git a/content_entity_example/src/Form/ContactForm.php b/content_entity_example/src/Form/ContactForm.php index 2a77cb4..e0b647e 100755 --- a/content_entity_example/src/Form/ContactForm.php +++ b/content_entity_example/src/Form/ContactForm.php @@ -8,6 +8,7 @@ namespace Drupal\content_entity_example\Form; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Language\Language; +use Drupal\Core\Form\FormStateInterface; /** * Form controller for the content_entity_example entity edit forms. @@ -19,7 +20,7 @@ class ContactForm extends ContentEntityForm { /** * Overrides Drupal\Core\Entity\EntityFormController::buildForm(). */ - public function buildForm(array $form, array &$form_state) { + public function buildForm(array $form, FormStateInterface $form_state) { /* @var $entity \Drupal\content_entity_example\Entity\Contact */ $form = parent::buildForm($form, $form_state); $entity = $this->entity; @@ -37,10 +38,10 @@ class ContactForm extends ContentEntityForm { /** * Overrides \Drupal\Core\Entity\EntityFormController::submit(). */ - public function submit(array $form, array &$form_state) { + public function submit(array $form, FormStateInterface $form_state) { // Build the entity object from the submitted values. $entity = parent::submit($form, $form_state); - $form_state['redirect_route']['route_name'] = 'content_entity_example.contact_list'; + $form_state->setRedirect('content_entity_example.contact_list'); return $entity; } @@ -48,7 +49,7 @@ class ContactForm extends ContentEntityForm { /** * Overrides Drupal\Core\Entity\EntityFormController::save(). */ - public function save(array $form, array &$form_state) { + public function save(array $form, FormStateInterface $form_state) { $entity = $this->entity; $entity->save(); } diff --git a/content_entity_example/src/Form/ContactSettingsForm.php b/content_entity_example/src/Form/ContactSettingsForm.php index a8d02fc..6783092 100755 --- a/content_entity_example/src/Form/ContactSettingsForm.php +++ b/content_entity_example/src/Form/ContactSettingsForm.php @@ -7,6 +7,7 @@ namespace Drupal\content_entity_example\Form; use Drupal\Core\Form\FormBase; +use Drupal\Core\Form\FormStateInterface; /** * Class ContentEntityExampleSettingsForm. @@ -15,7 +16,7 @@ use Drupal\Core\Form\FormBase; */ class ContactSettingsForm extends FormBase { /** - * Returns a unique string identifying the form. + * Returns a unique string idenying the form. * * @return string * The unique string identifying the form. @@ -27,12 +28,12 @@ class ContactSettingsForm extends FormBase { /** * Form submission handler. * - * @param array $form + * @param FormStateInterface $form * An associative array containing the structure of the form. * @param array $form_state * An associative array containing the current state of the form. */ - public function submitForm(array &$form, array &$form_state) { + public function submitForm(array &$form, FormStateInterface $form_state) { // Empty implementation of the abstract submit class. } @@ -44,10 +45,10 @@ class ContactSettingsForm extends FormBase { * * @param array $form * An associative array containing the structure of the form. - * @param array $form_state + * @param FormStateInterface $form_state * An associative array containing the current state of the form. */ - public function buildForm(array $form, array &$form_state) { + public function buildForm(array $form, FormStateInterface $form_state) { $form['contact_settings']['#markup'] = 'Settings form for ContentEntityExample. Manage field settings here.'; return $form; } diff --git a/content_entity_example/src/Tests/ContentEntityExampleTest.php b/content_entity_example/src/Tests/ContentEntityExampleTest.php index e2ca4b5..591b35b 100755 --- a/content_entity_example/src/Tests/ContentEntityExampleTest.php +++ b/content_entity_example/src/Tests/ContentEntityExampleTest.php @@ -17,8 +17,7 @@ use Drupal\simpletest\WebTestBase; */ class ContentEntityExampleTest extends WebTestBase { - public static $modules = array('content_entity_example', 'block', - 'entity_reference'); + public static $modules = array('content_entity_example', 'block'); protected $webUser; @@ -54,14 +53,14 @@ class ContentEntityExampleTest extends WebTestBase { public function testContentEntityExample() { // Anonymous User should not see the link to the listing. - $this->assertNoText(t('Contacts Listing')); + $this->assertNoText(t('Content Entity Example: Contacts Listing')); $this->drupalLogin($this->webUser); // Web_user user has the right to view listing. - $this->assertLink(t('Contacts Listing')); + $this->assertLink(t('Content Entity Example: Contacts Listing')); - $this->clickLink(t('Contacts Listing')); + $this->clickLink(t('Content Entity Example: Contacts Listing')); // WebUser can add entity content. $this->assertLink(t('Add Contact')); @@ -79,7 +78,7 @@ class ContentEntityExampleTest extends WebTestBase { $edit = array( 'name[0][value]' => 'test name', 'first_name[0][value]' => 'test first name', - 'gender[0][value]' => 'test gender', + 'gender' => 'male', ); $this->drupalPostForm(NULL, $edit, t('Save')); @@ -92,7 +91,7 @@ class ContentEntityExampleTest extends WebTestBase { // Entity shown. $this->assertText(t('test name')); $this->assertText(t('test first name')); - $this->assertText(t('test gender')); + $this->assertText(t('male')); $this->assertLink(t('Add Contact')); $this->assertLink(t('Edit')); $this->assertLink(t('Delete'));