Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a config entity for the module to save site info.
- Loading branch information
Showing
9 changed files
with
384 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
sitedash.sitedash.*: | ||
type: config_entity | ||
label: 'Sitedash config' | ||
mapping: | ||
id: | ||
type: string | ||
label: 'ID' | ||
label: | ||
type: label | ||
label: 'Label' | ||
siteUrl: | ||
type: string | ||
label: 'Sitedash Site URL' | ||
siteAPIUrl: | ||
type: string | ||
label: 'Sitedash Site API endpoint URL' | ||
token: | ||
type: string | ||
label: 'Sitedash Site API token' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
entity.sitedash.add_form: | ||
route_name: 'entity.sitedash.add_form' | ||
title: 'Add sitedash' | ||
appears_on: | ||
- entity.sitedash.collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
entity.sitedash.collection: | ||
title: 'Sitedash' | ||
parent: system.admin_config_system | ||
description: 'Configure sitedash' | ||
route_name: entity.sitedash.collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Drupal\sitedash\Controller; | ||
|
||
use Drupal\Core\Config\Entity\ConfigEntityListBuilder; | ||
use Drupal\Core\Entity\EntityInterface; | ||
|
||
/** | ||
* Provides a listing of Sitedash. | ||
*/ | ||
class SitedashListBuilder extends ConfigEntityListBuilder { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildHeader() { | ||
$header['label'] = $this->t('Sitedash'); | ||
$header['id'] = $this->t('Machine name'); | ||
$header['siteUrl'] = $this->t('Site URL'); | ||
return $header + parent::buildHeader(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildRow(EntityInterface $entity) { | ||
$row['label'] = $entity->label(); | ||
$row['id'] = $entity->id(); | ||
$row['siteUrl'] = $entity->siteUrl; | ||
|
||
// You probably want a few more properties here... | ||
|
||
return $row + parent::buildRow($entity); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Drupal\sitedash\Entity; | ||
|
||
use Drupal\Core\Config\Entity\ConfigEntityBase; | ||
use Drupal\sitedash\SitedashInterface; | ||
|
||
/** | ||
* Defines the Sitedash entity. | ||
* | ||
* @ConfigEntityType( | ||
* id = "sitedash", | ||
* label = @Translation("Sitedash"), | ||
* handlers = { | ||
* "list_builder" = "Drupal\sitedash\Controller\SitedashListBuilder", | ||
* "form" = { | ||
* "add" = "Drupal\sitedash\Form\SitedashForm", | ||
* "edit" = "Drupal\sitedash\Form\SitedashForm", | ||
* "delete" = "Drupal\sitedash\Form\SitedashDeleteForm", | ||
* } | ||
* }, | ||
* config_prefix = "sitedash", | ||
* admin_permission = "administer site configuration", | ||
* entity_keys = { | ||
* "id" = "id", | ||
* "label" = "label", | ||
* }, | ||
* config_export = { | ||
* "id", | ||
* "label", | ||
* "siteUrl", | ||
* "siteAPIUrl", | ||
* "token" | ||
* }, | ||
* links = { | ||
* "edit-form" = "/admin/config/system/sitedash/{sitedash}", | ||
* "delete-form" = "/admin/config/system/sitedash/{sitedash}/delete", | ||
* } | ||
* ) | ||
*/ | ||
class Sitedash extends ConfigEntityBase implements SitedashInterface { | ||
|
||
/** | ||
* The Sitedash ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* The Sitedash label. | ||
* | ||
* @var string | ||
*/ | ||
protected $label; | ||
|
||
/** | ||
* The Sitedash Site URL. | ||
* | ||
* @var string | ||
*/ | ||
public $siteUrl; | ||
|
||
/** | ||
* The Sitedash Site API Endpoint URL. | ||
* | ||
* @var string | ||
*/ | ||
public $siteAPIUrl; | ||
|
||
/** | ||
* The Sitedash Site API token. | ||
* | ||
* @var string | ||
*/ | ||
public $token; | ||
|
||
// Your specific configuration property get/set methods go here, | ||
// implementing the interface. | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSiteUrl() { | ||
return $this->siteUrl; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSiteAPIUrl() { | ||
return $this->siteAPIUrl; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getToken() { | ||
return $this->token; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Drupal\sitedash\Form; | ||
|
||
use Drupal\Core\Entity\EntityConfirmFormBase; | ||
use Drupal\Core\Url; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Builds the form to delete an Example. | ||
*/ | ||
|
||
class SitedashDeleteForm extends EntityConfirmFormBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getQuestion() { | ||
return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCancelUrl() { | ||
return new Url('entity.sitedash.collection'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfirmText() { | ||
return $this->t('Delete'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state) { | ||
$this->entity->delete(); | ||
$this->messenger()->addMessage($this->t('Entity %label has been deleted.', ['%label' => $this->entity->label()])); | ||
|
||
$form_state->setRedirectUrl($this->getCancelUrl()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Drupal\sitedash\Form; | ||
|
||
use Drupal\Core\Entity\EntityForm; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Form handler for the Sitedash add and edit forms. | ||
*/ | ||
class SitedashForm extends EntityForm { | ||
|
||
/** | ||
* Constructs an SitedashForm object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entityTypeManager. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entityTypeManager) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('entity_type.manager') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function form(array $form, FormStateInterface $form_state) { | ||
$form = parent::form($form, $form_state); | ||
|
||
$sitedash = $this->entity; | ||
|
||
// Change page title for the edit operation. | ||
if ($this->operation == 'edit') { | ||
$form['#title'] = $this->t('Edit sitedash: @label', ['@label' => $sitedash->label()]); | ||
} | ||
|
||
$form['label'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('Site Name'), | ||
'#maxlength' => 255, | ||
'#default_value' => $sitedash->label(), | ||
'#description' => $this->t("Label for the Sitedash Site."), | ||
'#required' => TRUE, | ||
]; | ||
$form['id'] = [ | ||
'#type' => 'machine_name', | ||
'#default_value' => $sitedash->id(), | ||
'#machine_name' => [ | ||
'exists' => [$this, 'exist'], | ||
], | ||
'#disabled' => !$sitedash->isNew(), | ||
]; | ||
$form['siteUrl'] = [ | ||
'#type' => 'url', | ||
'#title' => $this->t('Site URL'), | ||
'#size' => 50, | ||
'#default_value' => $sitedash->getSiteUrl(), | ||
'#description' => $this->t("The Site URL."), | ||
'#required' => TRUE, | ||
]; | ||
$form['siteAPIUrl'] = [ | ||
'#type' => 'url', | ||
'#title' => $this->t('Site API end point URL'), | ||
'#size' => 100, | ||
'#default_value' => $sitedash->getSiteAPIUrl(), | ||
'#description' => $this->t("API endpoint of the connector site."), | ||
'#required' => TRUE, | ||
]; | ||
$form['token'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('API Token'), | ||
'#maxlength' => 255, | ||
'#default_value' => $sitedash->getToken(), | ||
'#description' => $this->t("API token for the site."), | ||
'#required' => TRUE, | ||
]; | ||
|
||
// You will need additional form elements for your custom properties. | ||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(array $form, FormStateInterface $form_state) { | ||
$sitedash = $this->entity; | ||
$status = $sitedash->save(); | ||
|
||
if ($status === SAVED_NEW) { | ||
$this->messenger()->addMessage($this->t('The %label Sitedash Site created.', [ | ||
'%label' => $sitedash->label(), | ||
])); | ||
} | ||
else { | ||
$this->messenger()->addMessage($this->t('The %label Sitedash Site updated.', [ | ||
'%label' => $sitedash->label(), | ||
])); | ||
} | ||
|
||
$form_state->setRedirect('entity.sitedash.collection'); | ||
} | ||
|
||
/** | ||
* Helper function to check whether an Sitedash configuration entity exists. | ||
*/ | ||
public function exist($id) { | ||
$entity = $this->entityTypeManager->getStorage('sitedash')->getQuery() | ||
->condition('id', $id) | ||
->execute(); | ||
return (bool) $entity; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Drupal\sitedash; | ||
|
||
use Drupal\Core\Config\Entity\ConfigEntityInterface; | ||
|
||
/** | ||
* Provides an interface defining an Example entity. | ||
*/ | ||
interface SitedashInterface extends ConfigEntityInterface { | ||
// Add get/set methods for your configuration properties here. | ||
public function getSiteUrl(); | ||
public function getToken(); | ||
public function getSiteAPIUrl(); | ||
} |