CheckinMerciLineItem.php in MERCI (Manage Equipment Reservations, Checkout and Inventory) 8.2
File
modules/merci_line_item/src/Plugin/Action/CheckinMerciLineItem.php
View source
<?php
namespace Drupal\merci_line_item\Plugin\Action;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Datetime\DrupalDateTime;
class CheckinMerciLineItem extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
use DependencyTrait;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function execute($entity = NULL) {
$checkin_date = new DrupalDateTime($this->configuration['checkin_date']);
$checkin_date
->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$checkin_string = $checkin_date
->format(DATETIME_DATETIME_STORAGE_FORMAT);
$checkin_date_field = 'field_checkin';
$entity
->set($checkin_date_field, $checkin_string);
$entity
->set('field_reservation_status', 'checked_in');
$entity
->save();
}
public function defaultConfiguration() {
return [
'checkin_date' => 'now',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['checkin_date'] = [
'#type' => 'textfield',
'#title' => t('Default checkin date and time.'),
'#default_value' => $this->configuration['checkin_date'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['checkin_date'] = $form_state
->getValue('checkin_date');
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $object
->access('update', $account, TRUE);
return $return_as_object ? $access : $access
->isAllowed();
}
}