PhotosAccessPasswordForm.php in Album Photos 6.0.x
File
photos_access/src/Form/PhotosAccessPasswordForm.php
View source
<?php
namespace Drupal\photos_access\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class PhotosAccessPasswordForm extends FormBase {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function getFormId() {
return 'photos_access_password';
}
public function buildForm(array $form, FormStateInterface $form_state, $node = NULL) {
$form['pass'] = [
'#type' => 'password',
'#title' => $this
->t('Please enter album password'),
'#attributes' => [
'autocomplete' => 'off',
],
];
$form['nid'] = [
'#type' => 'value',
'#value' => $node
->id(),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$node = $this->connection
->query("SELECT pass, nid FROM {photos_access_album} WHERE nid = :nid AND pass = :pass", [
':nid' => $form_state
->getValue('nid'),
':pass' => md5($form_state
->getValue('pass')),
])
->fetchObject();
if (isset($node->pass)) {
$_SESSION[$node->nid . '_' . session_id()] = $node->pass;
$redirect_url = Url::fromUri('base:node/' . $node->nid)
->toString();
return new RedirectResponse($redirect_url);
}
else {
$form_state
->setErrorByName('pass', $this
->t('Password required'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}