Skip to content

Commit

Permalink
Adding a config entity for the module to save site info.
Browse files Browse the repository at this point in the history
  • Loading branch information
swarad07 committed Dec 3, 2021
1 parent 8971958 commit 8706642
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 1 deletion.
20 changes: 20 additions & 0 deletions config/schema/sitedash.schema.yml
@@ -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'

5 changes: 5 additions & 0 deletions sitedash.links.action.yml
@@ -0,0 +1,5 @@
entity.sitedash.add_form:
route_name: 'entity.sitedash.add_form'
title: 'Add sitedash'
appears_on:
- entity.sitedash.collection
5 changes: 5 additions & 0 deletions sitedash.links.menu.yml
@@ -0,0 +1,5 @@
entity.sitedash.collection:
title: 'Sitedash'
parent: system.admin_config_system
description: 'Configure sitedash'
route_name: entity.sitedash.collection
34 changes: 33 additions & 1 deletion sitedash.routing.yml
Expand Up @@ -4,4 +4,36 @@ sitedash.content:
_controller: '\Drupal\sitedash\Controller\SiteDashController::content'
_title: 'Site Dashboard'
requirements:
_permission: 'access content'
_permission: 'administer site configuration'

entity.sitedash.collection:
path: '/admin/config/system/sitedash'
defaults:
_entity_list: 'sitedash'
_title: 'Sitedash configuration'
requirements:
_permission: 'administer site configuration'

entity.sitedash.add_form:
path: '/admin/config/system/sitedash/add'
defaults:
_entity_form: 'sitedash.add'
_title: 'Add sitedash'
requirements:
_permission: 'administer site configuration'

entity.sitedash.edit_form:
path: '/admin/config/system/sitedash/{sitedash}'
defaults:
_entity_form: 'sitedash.edit'
_title: 'Edit sitedash'
requirements:
_permission: 'administer site configuration'

entity.sitedash.delete_form:
path: '/admin/config/system/sitedash/{sitedash}/delete'
defaults:
_entity_form: 'sitedash.delete'
_title: 'Delete sitedash'
requirements:
_permission: 'administer site configuration'
36 changes: 36 additions & 0 deletions src/Controller/SitedashListBuilder.php
@@ -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);
}

}
101 changes: 101 additions & 0 deletions src/Entity/Sitedash.php
@@ -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;
}

}
46 changes: 46 additions & 0 deletions src/Form/SitedashDeleteForm.php
@@ -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());
}

}
123 changes: 123 additions & 0 deletions src/Form/SitedashForm.php
@@ -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;
}

}
15 changes: 15 additions & 0 deletions src/SitedashInterface.php
@@ -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();
}

0 comments on commit 8706642

Please sign in to comment.