Skip to content
Snippets Groups Projects
Commit 9072b8df authored by Ronan Dowling's avatar Ronan Dowling
Browse files

Add schedule entity boilerplate

parent 6796d15d
No related branches found
No related tags found
No related merge requests found
entity.backup_migrate_schedule.add:
route_name: 'entity.backup_migrate_schedule.add'
title: 'Add Schedule'
appears_on:
- entity.backup_migrate_schedule.collection
......@@ -18,3 +18,8 @@ backup_migrate.advanced_backup:
route_name: backup_migrate.advanced_backup
parent_id: backup_migrate.quick_backup
backup_migrate.schedule:
title: Schedules
route_name: entity.backup_migrate_schedule.collection
base_route: backup_migrate.quick_backup
......@@ -22,3 +22,35 @@ backup_migrate.restore:
requirements:
_permission: 'restore from backup'
entity.backup_migrate_schedule.collection:
path: '/admin/config/development/backup_migrate/schedule'
defaults:
_entity_list: 'backup_migrate_schedule'
_title: 'Schedule'
requirements:
_permission: 'restore from backup'
entity.backup_migrate_schedule.add:
path: '/admin/config/development/backup_migrate/schedule/add'
defaults:
_entity_form: backup_migrate_schedule.default
_title: 'Add schedule'
requirements:
_permission: 'administer site configuration'
entity.backup_migrate_schedule.edit_form:
path: '/admin/config/development/backup_migrate/schedule/edit/{backup_migrate_schedule}'
defaults:
_entity_form: backup_migrate_schedule.default
_title: 'Edit schedule'
requirements:
_permission: 'administer site configuration'
entity.backup_migrate_schedule.delete_form:
path: '/admin/config/development/backup_migrate/schedule/edit/{backup_migrate_schedule}'
defaults:
_entity_form: backup_migrate_schedule.delete
_title: 'Delete schedule'
requirements:
_permission: 'administer site configuration'
id: daily_schedule
label: Daily Schedule
......@@ -23,11 +23,16 @@ backup_migrate.schedule.*:
type: config_entity
label: 'Backup and Migrate schedule'
mapping:
id:
type: string
label: 'ID'
name:
type: string
label:
type: label
label: 'Label'
uuid:
type: string
backup_migrate.source.*:
type: config_entity
......
<?php
/**
* @file
* Contains \Drupal\backup_migrate\Entity\Schedule.
*/
namespace Drupal\backup_migrate\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\backup_migrate\ScheduleInterface;
/**
* Defines the Schedule entity.
*
* @ConfigEntityType(
* id = "backup_migrate_schedule",
* label = @Translation("Schedule"),
* module = "backup_migrate",
* config_prefix = "schedule",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* handlers = {
* "list_builder" = "Drupal\backup_migrate\ScheduleListBuilder",
* "form" = {
* "default" = "Drupal\backup_migrate\Form\ScheduleForm",
* "delete" = "Drupal\backup_migrate\Form\ScheduleDeleteForm"
* },
* },
* links = {
* "edit-form" = "/admin/config/development/backup_migrate/schedule/edit/{schedule}",
* "delete-form" = "/admin/config/development/backup_migrate/schedule/delete/{schedule}",
* "collection" = "/admin/config/development/backup_migrate/schedule",
* },
* )
*/
class Schedule extends ConfigEntityBase implements ScheduleInterface {
/**
* The Schedule ID.
*
* @var string
*/
protected $id;
/**
* The Schedule label.
*
* @var string
*/
protected $label;
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\Form\ScheduleDeleteForm.
*/
namespace Drupal\backup_migrate\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete Schedule entities.
*/
class ScheduleDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %name?', array('%name' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.backup_migrate_schedule.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(
$this->t('content @type: deleted @label.',
[
'@type' => $this->entity->bundle(),
'@label' => $this->entity->label()
]
)
);
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\Form\ScheduleForm.
*/
namespace Drupal\backup_migrate\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Class ScheduleForm.
*
* @package Drupal\backup_migrate\Form
*/
class ScheduleForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$backup_migrate_schedule = $this->entity;
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $backup_migrate_schedule->label(),
'#description' => $this->t("Label for the Schedule."),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $backup_migrate_schedule->id(),
'#machine_name' => array(
'exists' => '\Drupal\backup_migrate\Entity\Schedule::load',
),
'#disabled' => !$backup_migrate_schedule->isNew(),
);
/* You will need additional form elements for your custom properties. */
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$backup_migrate_schedule = $this->entity;
$status = $backup_migrate_schedule->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Schedule.', [
'%label' => $backup_migrate_schedule->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Schedule.', [
'%label' => $backup_migrate_schedule->label(),
]));
}
$form_state->setRedirectUrl($backup_migrate_schedule->urlInfo('collection'));
}
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\ScheduleInterface.
*/
namespace Drupal\backup_migrate;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining Schedule entities.
*/
interface ScheduleInterface extends ConfigEntityInterface {
// Add get/set methods for your configuration properties here.
}
<?php
/**
* @file
* Contains \Drupal\backup_migrate\ScheduleListBuilder.
*/
namespace Drupal\backup_migrate;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Schedule entities.
*/
class ScheduleListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Schedule name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->hasLinkTemplate('edit-form')) {
$operations['edit'] = array(
'title' => t('Edit schedule'),
'weight' => 20,
'url' => $entity->urlInfo('edit-form'),
);
}
if ($entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = array(
'title' => t('Delete schedule'),
'weight' => 30,
'url' => $entity->urlInfo('delete-form'),
);
}
return $operations;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
drupal_set_message(t('The schedule settings have been updated.'));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment