diff --git a/config/schema/sitedash.schema.yml b/config/schema/sitedash.schema.yml new file mode 100644 index 0000000..cd1cb0e --- /dev/null +++ b/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' + diff --git a/sitedash.links.action.yml b/sitedash.links.action.yml new file mode 100644 index 0000000..be6c51b --- /dev/null +++ b/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 diff --git a/sitedash.links.menu.yml b/sitedash.links.menu.yml new file mode 100644 index 0000000..d0ef905 --- /dev/null +++ b/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 diff --git a/sitedash.routing.yml b/sitedash.routing.yml index 67fd790..cf8f246 100644 --- a/sitedash.routing.yml +++ b/sitedash.routing.yml @@ -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' diff --git a/src/Controller/SitedashListBuilder.php b/src/Controller/SitedashListBuilder.php new file mode 100644 index 0000000..5940bf6 --- /dev/null +++ b/src/Controller/SitedashListBuilder.php @@ -0,0 +1,36 @@ +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); + } + +} diff --git a/src/Entity/Sitedash.php b/src/Entity/Sitedash.php new file mode 100644 index 0000000..5aa04ba --- /dev/null +++ b/src/Entity/Sitedash.php @@ -0,0 +1,101 @@ +siteUrl; + } + + /** + * {@inheritdoc} + */ + public function getSiteAPIUrl() { + return $this->siteAPIUrl; + } + + /** + * {@inheritdoc} + */ + public function getToken() { + return $this->token; + } + +} diff --git a/src/Form/SitedashDeleteForm.php b/src/Form/SitedashDeleteForm.php new file mode 100644 index 0000000..258c5e7 --- /dev/null +++ b/src/Form/SitedashDeleteForm.php @@ -0,0 +1,46 @@ +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()); + } + +} diff --git a/src/Form/SitedashForm.php b/src/Form/SitedashForm.php new file mode 100644 index 0000000..e6f8221 --- /dev/null +++ b/src/Form/SitedashForm.php @@ -0,0 +1,123 @@ +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; + } + +} diff --git a/src/SitedashInterface.php b/src/SitedashInterface.php new file mode 100644 index 0000000..80c5d1d --- /dev/null +++ b/src/SitedashInterface.php @@ -0,0 +1,15 @@ +