Skip to content

Commit

Permalink
PCC-57: PCC Site Configurations entity. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvir249 authored May 16, 2024
1 parent 8369415 commit b4c8514
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 0 deletions.
19 changes: 19 additions & 0 deletions config/schema/pcx_connect.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pcx_connect.pcc_site.*:
type: config_entity
label: 'PCC Site config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'PCC Site Name'
site_key:
type: string
label: 'PCC Site Key'
site_token:
type: string
label: 'PCC Site Token'
site_url:
type: string
label: 'PCC Site URL'
5 changes: 5 additions & 0 deletions pcx_connect.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entity.pcc_site.add_form:
route_name: 'entity.pcc_site.add_form'
title: 'Add PCC Site'
appears_on:
- entity.pcc_site.collection
5 changes: 5 additions & 0 deletions pcx_connect.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entity.pcc_site.collection:
title: 'PCC Sites'
parent: system.admin_structure
description: 'Configure PCC Sites'
route_name: entity.pcc_site.collection
3 changes: 3 additions & 0 deletions pcx_connect.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer pcc configurations:
title: 'PCC site configuration'
restrict access: true
32 changes: 32 additions & 0 deletions pcx_connect.routing.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
entity.pcc_site.collection:
path: '/admin/structure/pcc_sites'
defaults:
_entity_list: 'pcc_site'
_title: 'PCC Sites'
requirements:
_permission: 'administer pcc configurations'

entity.pcc_site.add_form:
path: '/admin/structure/pcc_sites/add'
defaults:
_entity_form: 'pcc_site.add'
_title: 'Add PCC Site'
requirements:
_permission: 'administer pcc configurations'

entity.pcc_site.edit_form:
path: '/admin/structure/pcc_sites/{pcc_site}'
defaults:
_entity_form: 'pcc_site.edit'
_title: 'Edit PCC Site'
requirements:
_permission: 'administer pcc configurations'

entity.pcc_site.delete_form:
path: '/admin/structure/pcc_sites/{pcc_site}/delete'
defaults:
_entity_form: 'pcc_site.delete'
_title: 'Delete PCC Site'
requirements:
_permission: 'administer pcc configurations'

pcx_connect.api_status:
path: '/api/pantheoncloud/status'
defaults:
Expand Down
33 changes: 33 additions & 0 deletions src/Controller/PccSiteListBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\pcx_connect\Controller;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;

/**
* Provides a listing of PCC Sites.
*/
class PccSiteListBuilder extends ConfigEntityListBuilder {

/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('ID');
$header['label'] = $this->t('Site name');
$header['site_url'] = $this->t('Site Url');
return $header + parent::buildHeader();
}

/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity->id();
$row['label'] = $entity->label();
$row['site_url'] = $entity->get('site_url');
return $row + parent::buildRow($entity);
}

}
57 changes: 57 additions & 0 deletions src/Entity/PccSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Drupal\pcx_connect\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\pcx_connect\PccSiteInterface;

/**
* Defines the PCC Site entity.
*
* @ConfigEntityType(
* id = "pcc_site",
* label = @Translation("PCC Site"),
* handlers = {
* "list_builder" = "Drupal\pcx_connect\Controller\PccSiteListBuilder",
* "form" = {
* "add" = "Drupal\pcx_connect\Form\PccSiteForm",
* "edit" = "Drupal\pcx_connect\Form\PccSiteForm",
* "delete" = "Drupal\pcx_connect\Form\PccSiteDeleteForm",
* }
* },
* config_prefix = "pcc_site",
* admin_permission = "administer pcc configurations",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* },
* config_export = {
* "id",
* "label",
* "site_key",
* "site_token",
* "site_url"
* },
* links = {
* "edit-form" = "/admin/config/system/pcc-sites/{pcc_site}",
* "delete-form" = "/admin/config/system/pcc-sites/{pcc_site}/delete",
* }
* )
*/
class PccSite extends ConfigEntityBase implements PccSiteInterface {

/**
* The PCC Site ID.
*
* @var string
*/
protected $id;

/**
* The pcc site label.
*
* @var string
*/
protected $label;

}
45 changes: 45 additions & 0 deletions src/Form/PccSiteDeleteForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\pcx_connect\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
* Builds the form to delete an PCC Site.
*/
class PccSiteDeleteForm 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.pcc_site.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('PCC Site %label has been deleted.', ['%label' => $this->entity->label()]));

$form_state->setRedirectUrl($this->getCancelUrl());
}

}
124 changes: 124 additions & 0 deletions src/Form/PccSiteForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Drupal\pcx_connect\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 PCC Site add and edit forms.
*/
class PccSiteForm extends EntityForm {

/**
* Constructs an PccSiteForm 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);
$pcc_site = $this->entity;

$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Site Name'),
'#maxlength' => 255,
'#default_value' => $pcc_site->label(),
'#description' => $this->t("Name for the PCC Site."),
'#required' => TRUE,
];

$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $pcc_site->id(),
'#machine_name' => [
'exists' => [$this, 'exist'],
],
'#disabled' => !$pcc_site->isNew(),
];

$form['site_key'] = [
'#type' => 'textfield',
'#title' => $this->t('Site Key'),
'#maxlength' => 255,
'#default_value' => $pcc_site->get('site_key') ?: '',
'#description' => $this->t("Site Key for PCC Site."),
'#required' => TRUE,
];

$form['site_token'] = [
'#type' => 'textfield',
'#title' => $this->t('Site Token'),
'#maxlength' => 255,
'#default_value' => $pcc_site->get('site_token') ?: '',
'#description' => $this->t("Site token for PCC Site."),
'#required' => TRUE,
];

$form['site_url'] = [
'#type' => 'url',
'#title' => $this->t('Site URL'),
'#maxlength' => 255,
'#default_value' => $pcc_site->get('site_url') ?: '',
'#description' => $this->t("Site URL for PCC Site."),
'#required' => TRUE,
'#attributes' => [
'placeholder' => 'https://',
],
];

// You will need additional form elements for your custom properties.
return $form;
}

/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$pcc_site = $this->entity;
$status = $pcc_site->save();

if ($status === SAVED_NEW) {
$this->messenger()->addMessage($this->t('The %label PCC Site is created.', [
'%label' => $pcc_site->label(),
]));
}
else {
$this->messenger()->addMessage($this->t('The %label PCC Site is updated.', [
'%label' => $pcc_site->label(),
]));
}

$form_state->setRedirect('entity.pcc_site.collection');
}

/**
* Helper function to check whether an PCC Site configuration entity exists.
*/
public function exist($id) {
$entity = $this->entityTypeManager->getStorage('pcc_site')->getQuery()
->condition('id', $id)
->execute();
return (bool) $entity;
}

}
12 changes: 12 additions & 0 deletions src/PccSiteInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Drupal\pcx_connect;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
* Provides an interface defining an PCC Site entity.
*/
interface PccSiteInterface extends ConfigEntityInterface {

}

0 comments on commit b4c8514

Please sign in to comment.