diff --git a/config_entity_example/config/config_entity_example.robot.marvin.yml b/config_entity_example/config/config_entity_example.robot.marvin.yml new file mode 100644 index 0000000..eceaf86 --- /dev/null +++ b/config_entity_example/config/config_entity_example.robot.marvin.yml @@ -0,0 +1,12 @@ +# This file defines a default config entity. This allows the module to include +# config entities that are present 'out of the box'. +# These can be edited by the user. The modified version is saved to the site +# config folder, which this original copy remains untouched. + +# For this system to work, we have to define our config schema in +# config/config_entity_example.robot.marvin.yml. + +# The id of the config entity. +id: marvin +# Our properties follow. +label: 'Marvin, the paranoid android' diff --git a/config_entity_example/config/schema/config_entity_example.schema.yml b/config_entity_example/config/schema/config_entity_example.schema.yml new file mode 100644 index 0000000..38fcdce --- /dev/null +++ b/config_entity_example/config/schema/config_entity_example.schema.yml @@ -0,0 +1,19 @@ +# Schema for the configuration files of the Config Entity Example module. + +# This schema tells the config system how to read our config YML files. +# See for example the file config/config_entity_example.robot.marvin.yml, which +# contains our default config entity. + +config_entity_example.robot.*: + type: mapping + label: 'Robot' + mapping: + id: + type: string + label: 'Robot id' + uuid: + type: string + label: 'UUID' + label: + type: label + label: 'Label' diff --git a/config_entity_example/config_entity_example.info.yml b/config_entity_example/config_entity_example.info.yml new file mode 100644 index 0000000..56127ec --- /dev/null +++ b/config_entity_example/config_entity_example.info.yml @@ -0,0 +1,7 @@ +name: 'Config entity example' +type: module +description: 'TODO: Description of module' +package: Example modules +core: 8.x +files: + - tests/config_entity_example.test diff --git a/config_entity_example/config_entity_example.local_actions.yml b/config_entity_example/config_entity_example.local_actions.yml new file mode 100644 index 0000000..79dd2a6 --- /dev/null +++ b/config_entity_example/config_entity_example.local_actions.yml @@ -0,0 +1,5 @@ +config_entity_example_add_action: + route_name: robot.add + title: 'Add robot' + appears_on: + - robot.list diff --git a/config_entity_example/config_entity_example.module b/config_entity_example/config_entity_example.module new file mode 100644 index 0000000..a59d14b --- /dev/null +++ b/config_entity_example/config_entity_example.module @@ -0,0 +1,24 @@ + array( + 'title' => t('Administer robots'), + 'description' => t('Create and edit robots.'), + ), + ); + + return $permissions; +} \ No newline at end of file diff --git a/config_entity_example/config_entity_example.routing.yml b/config_entity_example/config_entity_example.routing.yml new file mode 100644 index 0000000..066be4f --- /dev/null +++ b/config_entity_example/config_entity_example.routing.yml @@ -0,0 +1,38 @@ +# The routing.yml file defines the paths for our module. +# Here we define the paths for our entity type's admin UI. + +# This is the router item for listing all entities. +robot.list: + path: '/examples/config_entity_example' + defaults: + # TODO: explain by what magic '_entity_list' turns into using the entity list controller. + _entity_list: 'robot' + _title: 'Config Entity Example' + requirements: + _permission: 'administer robots' + +# This is the router item for adding an entity. +robot.add: + path: '/examples/config_entity_example/add' + defaults: + _title: 'Add robot' + _entity_form: robot.add + requirements: + _entity_create_access: robot + +robot.edit: + path: '/examples/config_entity_example/manage/{robot}' + defaults: + _title: 'Edit robot' + _entity_form: robot.edit + requirements: + # This uses our entity access controller. + _entity_access: robot.update + +robot.delete: + path: '/examples/config_entity_example/manage/{robot}/delete' + defaults: + _title: 'Delete robot' + _entity_form: robot.delete + requirements: + _entity_access: robot.delete diff --git a/config_entity_example/lib/Drupal/config_entity_example/Controller/RobotListController.php b/config_entity_example/lib/Drupal/config_entity_example/Controller/RobotListController.php new file mode 100644 index 0000000..2e153e5 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Controller/RobotListController.php @@ -0,0 +1,46 @@ +t('Robot'); + $header['machine_name'] = $this->t('Machine Name'); + return $header + parent::buildHeader(); + } + + /** + * Builds a row for an entity in the entity listing. + * + * @param EntityInterface $entity + * The entity for which to build the row. + * + * @return array + * A render array of the table row for displaying the entity. + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $this->getLabel($entity); + + $row['machine_name'] = $entity->id(); + + return $row + parent::buildRow($entity); + } + +} diff --git a/config_entity_example/lib/Drupal/config_entity_example/Entity/Robot.php b/config_entity_example/lib/Drupal/config_entity_example/Entity/Robot.php new file mode 100644 index 0000000..cc97403 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Entity/Robot.php @@ -0,0 +1,87 @@ + $this->entity->label() + )); + } + + /** + * The confirm text is used as a the text in the button that confirms the + * question posed by getQuestion(). + * + * @return string + */ + public function getConfirmText() { + return t('Delete Robot'); + } + + /** + * Provides the route name to go to if the user cancels the action. For entity + * delete forms, this is typically the route that points at the list + * controller. + * + * @return array + */ + public function getCancelRoute() { + return array( + 'route_name' => 'robot_list', + ); + } + + /** + * The submit handler for the confirm form. For entity delete forms, you use + * this to delete the entity in $this->entity. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * + * @return \Drupal\Core\Entity\EntityInterface|void + */ + public function submit(array $form, array &$form_state) { + // Delete the entity. + $this->entity->delete(); + + // Set a message that the entity was deleted. + drupal_set_message(t('Robot %label was deleted.', array( + '%label' => $this->entity->label(), + ))); + + // Redirect the user to the list controller when complete. + $form_state['redirect_route']['route_name'] = 'robot_list'; + } + +} diff --git a/config_entity_example/lib/Drupal/config_entity_example/Form/RobotEditForm.php b/config_entity_example/lib/Drupal/config_entity_example/Form/RobotEditForm.php new file mode 100644 index 0000000..e342f73 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Form/RobotEditForm.php @@ -0,0 +1,37 @@ +entity; + + // Build the form. + $form['label'] = array( + '#type' => 'textfield', + '#title' => t('Label'), + '#maxlength' => 255, + '#default_value' => $robot->label(), + '#required' => TRUE, + ); + $form['id'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#default_value' => $robot->id(), + '#machine_name' => array( + 'exists' => 'contact_category_load', + ), + '#disabled' => !$robot->isNew(), + ); + + // Return the form. + return $form; + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::actions(). + * + * To set the submit button text, we need to override actions(). + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * + * @return array + */ + protected function actions(array $form, array &$form_state) { + // Get the basic actins from the base class. + $actions = parent::actions($form, $form_state); + + // Change the submit button text. + $actions['submit']['#value'] = t('Save'); + + // Return the result. + return $actions; + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::validate(). + * + * @param array $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 validate(array $form, array &$form_state) { + parent::validate($form, $form_state); + + // Add code here to validate your config entity's form elements. + // Nothing to do here. + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::save(). + * + * Saves the entity. This is called after submit() has built the entity from + * the form values. Do not override submit() as save() is the preferred method + * for entity form controllers. + * + * @param array $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 save(array $form, array &$form_state) { + // Get the entity from the class variable. We don't need to do this, but + // it often makes the code easier to read. + $robot = $this->entity; + + // Drupal already populated the form values in the entity object. Each + // form field was saved as a public variable in the entity class. PHP allows + // Drupal to do this even if the method is not defined ahead of time. + $status = $robot->save(); + + // Grab the URL of the new entity. We'll use it in the message. + $uri = $robot->url(); + + if ($status == SAVED_UPDATED) { + // If we edited an existing entity... + drupal_set_message(t('Robot %label has been updated.', array('%label' => $robot->label()))); + watchdog('contact', 'Robot %label has been updated.', array('%label' => $robot->label()), WATCHDOG_NOTICE, l(t('Edit'), $uri . '/edit')); + } + else { + // If we created a new entity... + drupal_set_message(t('Robot %label has been added.', array('%label' => $robot->label()))); + watchdog('contact', 'Robot %label has been added.', array('%label' => $robot->label()), WATCHDOG_NOTICE, l(t('Edit'), $uri . '/edit')); + } + + // Redirect the user to the following path after the save optionation. + $form_state['route_redirect'] = new Url('robot.list'); + } + +} diff --git a/config_entity_example/lib/Drupal/config_entity_example/RobotAccessController.php b/config_entity_example/lib/Drupal/config_entity_example/RobotAccessController.php new file mode 100644 index 0000000..f5c2ebc --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/RobotAccessController.php @@ -0,0 +1,40 @@ +