-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PCC-57: PCC Site Configurations entity. (#2)
- Loading branch information
Showing
10 changed files
with
335 additions
and
0 deletions.
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,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' |
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.pcc_site.add_form: | ||
route_name: 'entity.pcc_site.add_form' | ||
title: 'Add PCC Site' | ||
appears_on: | ||
- entity.pcc_site.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.pcc_site.collection: | ||
title: 'PCC Sites' | ||
parent: system.admin_structure | ||
description: 'Configure PCC Sites' | ||
route_name: entity.pcc_site.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,3 @@ | ||
administer pcc configurations: | ||
title: 'PCC site configuration' | ||
restrict access: true |
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,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); | ||
} | ||
|
||
} |
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,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; | ||
|
||
} |
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,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()); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Drupal\pcx_connect; | ||
|
||
use Drupal\Core\Config\Entity\ConfigEntityInterface; | ||
|
||
/** | ||
* Provides an interface defining an PCC Site entity. | ||
*/ | ||
interface PccSiteInterface extends ConfigEntityInterface { | ||
|
||
} |