Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drupal 8.0.0 beta2 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bd_contact.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bd_contact.add:
route_name: bd_contact_add
title: 'New message'
appears_on:
- bd_contact_list
5 changes: 5 additions & 0 deletions bd_contact.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bd_contact_list:
route_name: bd_contact_list
title: 'BD Contact'
base_route: system.admin_content
weight: 10
4 changes: 0 additions & 4 deletions bd_contact.local_tasks.yml

This file was deleted.

21 changes: 2 additions & 19 deletions bd_contact.module
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
<?php

function bd_contact_menu() {
return array(
'admin/content/bd_contact' => array(
'title' => 'BD Contact Submissions',
'route_name' => 'bd_contact_list',
),
'admin/content/bd_contact/add' => array(
'title' => 'BD Contact',
'route_name' => 'bd_contact_add',
),
'admin/content/bd_contact/delete/%' => array(
'title' => 'Delete BD Contact Submission',
'route_name' => 'bd_contact_delete',
),
);
}

/**
* Implements hook_permission().
*/
function bd_contact_permission() {
return array(
'manage bd contact forms' => array(
'title' => t('Manage bd contact form submissions'),
'title' => t('Manage BD Contact form submissions'),
),
'use bd contact form' => array(
'title' => t('Use the bd contact form'),
'title' => t('Use the BD Contact form'),
),
);
}
8 changes: 8 additions & 0 deletions bd_contact.routing.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bd_contact_list:
path: 'admin/content/bd_contact'
defaults:
_title: BD Contact
_content: '\Drupal\bd_contact\AdminController::content'
requirements:
_permission: 'manage bd contact forms'
Expand All @@ -12,6 +13,13 @@ bd_contact_add:
requirements:
_permission: 'use bd contact form'

bd_contact_edit:
path: 'admin/content/bd_contact/edit/{id}'
defaults:
_form: '\Drupal\bd_contact\AddForm'
requirements:
_permission: 'use bd contact form'

bd_contact_delete:
path: 'admin/content/bd_contact/delete/{id}'
defaults:
Expand Down
39 changes: 28 additions & 11 deletions src/AddForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Drupal\bd_contact;

use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\String;

class AddForm implements FormInterface {

Expand All @@ -12,36 +14,51 @@ function getFormId() {
return 'bd_contact_add';
}

function buildForm(array $form, array &$form_state) {
function buildForm(array $form, FormStateInterface $form_state, $id = '') {
if ($id) {
$this->id = $id;
$bd_contact = BdContactStorage::get($this->id);
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($bd_contact) ? $bd_contact->name : '',
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => isset($bd_contact) ? $bd_contact->message : '',
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#value' => ($bd_contact) ? t('Edit') : t('Add'),
);
return $form;
}

function validateForm(array &$form, array &$form_state) {
function validateForm(array &$form, FormStateInterface $form_state) {
/* Nothing to validate on this form */
}

function submitForm(array &$form, array &$form_state) {
$name = $form_state['values']['name'];
$message = $form_state['values']['message'];
BdContactStorage::add(check_plain($name), check_plain($message));
function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state->getValues();

watchdog('bd_contact', 'BD Contact message from %name has been submitted.', array('%name' => $name));
drupal_set_message(t('Your message has been submitted'));
$form_state['redirect'] = 'admin/content/bd_contact';
return;
$name = $form_values['name'];
$message = $form_values['message'];
if (!empty($this->id)) {
BdContactStorage::edit($this->id, String::checkPlain($name), String::checkPlain($message));

\Drupal::logger('bd_contact')->notice('BD Contact message from %name has been edited.', array('%name' => $name));
drupal_set_message(t('Your message has been edited'));
}
else {
BdContactStorage::add(String::checkPlain($name), String::checkPlain($message));

\Drupal::logger('bd_contact')->notice('BD Contact message from %name has been submitted.', array('%name' => $name));
drupal_set_message(t('Your message has been submitted'));
}
$form_state->setRedirect('bd_contact_list');
}

}
8 changes: 5 additions & 3 deletions src/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Drupal\bd_contact;

class AdminController {
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;

class AdminController extends ControllerBase {

function content() {
$add_link = '<p>' . l(t('New message'), 'admin/content/bd_contact/add') . '</p>';

// Table header.
$header = array(
Expand All @@ -20,7 +22,7 @@ function content() {
foreach (BdContactStorage::getAll() as $id => $content) {
// Row with attributes on the row and some of its cells.
$rows[] = array(
'data' => array($id, $content->name, $content->message, l('Delete', "admin/content/bd_contact/delete/$id")),
'data' => array(\Drupal::l($id, new Url('bd_contact_edit', array('id' => $id))), $content->name, $content->message, \Drupal::l(t('Delete'), new Url('bd_contact_delete', array('id' => $id)))),
);
}

Expand Down
19 changes: 19 additions & 0 deletions src/BdContactStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@ static function exists($id) {
return (bool) $this->get($id);
}

static function get($id) {
$result = db_query('SELECT * FROM {bd_contact} WHERE id = :id', array(':id' => $id))->fetchAllAssoc('id');
if ($result) {
return $result[$id];
}
else {
return FALSE;
}
}

static function add($name, $message) {
db_insert('bd_contact')->fields(array(
'name' => $name,
'message' => $message,
))->execute();
}

static function edit($id, $name, $message) {
db_update('bd_contact')->fields(array(
'name' => $name,
'message' => $message,
))
->condition('id', $id)
->execute();
}

static function delete($id) {
db_delete('bd_contact')->condition('id', $id)->execute();
}
Expand Down
17 changes: 13 additions & 4 deletions src/DeleteForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\bd_contact;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class DeleteForm extends ConfirmFormBase {
Expand All @@ -25,17 +26,25 @@ function getCancelRoute() {
return new Url('bd_contact_list');
}

function buildForm(array $form, array &$form_state, $id = '') {
function getCancelUrl() {
return new Url('bd_contact_list');
}

function buildForm(array $form, FormStateInterface $form_state, $id = '') {
$this->id = $id;

return parent::buildForm($form, $form_state);
}

function submitForm(array &$form, array &$form_state) {
function validateForm(array &$form, FormStateInterface $form_state) {

}

function submitForm(array &$form, FormStateInterface $form_state) {
BdContactStorage::delete($this->id);
watchdog('bd_contact', 'Deleted BD Contact Submission with id %id.', array('%id' => $this->id));
\Drupal::logger('bd_contact')->notice('Deleted BD Contact Submission with id %id.', array('%id' => $this->id));
drupal_set_message(t('BD Contact submission %id has been deleted.', array('%id' => $this->id)));
$form_state['redirect'] = 'admin/content/bd_contact';
$form_state->setRedirect('bd_contact_list');
}

}