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.api.php b/config_entity_example/config_entity_example.api.php new file mode 100644 index 0000000..75b7830 --- /dev/null +++ b/config_entity_example/config_entity_example.api.php @@ -0,0 +1,6 @@ +t('Robot'); + $header['machine_name'] = $this->t('Machine Name'); + return $header + parent::buildHeader(); + } + + 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..961c607 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Entity/Robot.php @@ -0,0 +1,102 @@ + 'examples/config_entity_example/manage/' . $this->id(), + 'options' => array(), + ); + } + +} diff --git a/config_entity_example/lib/Drupal/config_entity_example/Form/RobotAddForm.php b/config_entity_example/lib/Drupal/config_entity_example/Form/RobotAddForm.php new file mode 100644 index 0000000..4bce116 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Form/RobotAddForm.php @@ -0,0 +1,31 @@ + $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 + * @param array $form_state + * @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..b91ee81 --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/Form/RobotEditForm.php @@ -0,0 +1,31 @@ +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 + * @param array $form_state + * @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 + * @param array $form_state + */ + 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. + */ + 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['redirect'] = 'examples/config_entity_example'; + } + +} 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..30cd54b --- /dev/null +++ b/config_entity_example/lib/Drupal/config_entity_example/RobotAccessController.php @@ -0,0 +1,40 @@ +