View source
<?php
namespace Drupal\heartbeat\Form;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Render\Renderer;
use Drupal\heartbeat\Entity;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\token\TreeBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HeartbeatTypeForm extends EntityForm {
protected $treeBuilder;
protected $renderer;
private $tokenTree;
protected $entityTypeManager;
protected $entityTypes;
private $treeAdded = false;
private $messageMap = array();
public static function create(ContainerInterface $container) {
return new static($container
->get('token.tree_builder'), $container
->get('renderer'), $container
->get('entity_type.manager'));
}
public function __construct(TreeBuilder $tree_builder, Renderer $renderer, EntityTypeManager $entityTypeManager) {
$this->treeBuilder = $tree_builder;
$this->renderer = $renderer;
$this->entityTypeManager = $entityTypeManager;
$this->tokenTree = $this->renderer
->render($this->treeBuilder
->buildAllRenderable([
'click_insert' => TRUE,
'show_restricted' => TRUE,
'show_nested' => FALSE,
]));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->entityTypes = Entity\Heartbeat::getEntityNames($this->entityTypeManager
->getDefinitions());
$doStuff = 'stuff';
return parent::buildForm($form, $form_state);
}
public function form(array $form, FormStateInterface $form_state) {
$form_state
->setCached(FALSE);
$heartbeat_type = $this->entity;
$form['#tree'] = TRUE;
$form['#attached']['library'] = 'heartbeat/treeTable';
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $heartbeat_type
->label(),
'#description' => $this
->t("Label for the Heartbeat Type."),
'#required' => TRUE,
);
$form['description'] = array(
'#type' => 'textfield',
'#title' => $this
->t('description'),
'#maxlength' => 255,
'#default_value' => $heartbeat_type
->getDescription(),
'#description' => $this
->t("Description of the Heartbeat Type"),
'#required' => TRUE,
);
$form['entity_type'] = array(
'#type' => 'select',
'#title' => $this
->t('Entity Type'),
'#description' => $this
->t("Primary Entity Type for this Heartbeat Type"),
'#options' => array(
$this->entityTypes,
),
'#required' => TRUE,
);
$bundles = $form_state
->get('entity_bundles');
$form['entity_bundles'] = array(
'#type' => 'container',
'#prefix' => '<div id="entity-bundles">',
'#suffix' => '</div>',
);
$form['entity_bundles']['getBundles'] = [
'#type' => 'submit',
'#value' => t('Getting bundles'),
'#submit' => array(
'::getBundlesForEntity',
),
'#ajax' => [
'callback' => '::getBundlesForEntity',
'wrapper' => 'entity-bundles',
'progress' => array(
'type' => 'throbber',
'message' => t('Getting bundles'),
),
],
];
$form['entity_bundles']['list'] = array(
'#type' => 'select',
'#title' => $this
->t('Entity Bundles'),
'#default_value' => $heartbeat_type
->getBundle(),
'#description' => $this
->t("Any bundles available to the specified entity"),
'#options' => $bundles,
);
$form['message'] = array(
'#type' => 'textfield',
'#title' => $this
->t('message'),
'#maxlength' => 255,
'#default_value' => $heartbeat_type
->getMessage(),
'#description' => $this
->t("The structure for messages of this type. Use !exclamation marks before fields and entities"),
'#required' => TRUE,
);
$form['perms'] = array(
'#type' => 'select',
'#title' => $this
->t('Permissions'),
'#default_value' => $heartbeat_type
->getPerms(),
'#description' => $this
->t("Default permissions to view Heartbeats of this type"),
'#options' => array(
Entity\HEARTBEAT_NONE => 'None',
Entity\HEARTBEAT_PRIVATE => 'Private',
Entity\HEARTBEAT_PUBLIC_TO_ADDRESSEE => 'Public to Addressee',
Entity\HEARTBEAT_PUBLIC_TO_CONNECTED => 'Public to Connected',
Entity\HEARTBEAT_PUBLIC_TO_ALL => 'Public to All',
),
'#required' => TRUE,
);
$form['group_type'] = array(
'#type' => 'select',
'#title' => $this
->t('Group Type'),
'#default_value' => 0,
'#description' => $this
->t("Type of group associated with Heartbeats of this type"),
'#options' => array(
Entity\HEARTBEAT_GROUP_NONE => 'None',
Entity\HEARTBEAT_GROUP_SINGLE => 'Single',
Entity\HEARTBEAT_GROUP_SUMMARY => 'Group',
),
'#required' => TRUE,
);
$form['variables'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Variables to map'),
'#prefix' => '<div id="Variables-fieldset-wrapper">',
'#suffix' => '</div>',
);
$messageArguments = $form_state
->get('data_hidden');
if ($messageArguments === NULL) {
$messageArguments = $this
->extractMessageArguments($heartbeat_type
->getMessage());
}
$argNum = count($messageArguments);
for ($i = 0; $i < $argNum; $i++) {
if (is_array($messageArguments) && $messageArguments[$i] != null) {
$variableValue = isset($heartbeat_type
->getVariables()[$i]) && !empty($heartbeat_type
->getVariables()[$i]) ? $heartbeat_type
->getVariables()[$i] : '';
$form['variables'][$i] = array(
'#type' => 'textfield',
'#title' => t($messageArguments[$i]),
'#description' => t('Map value to this variable'),
'#default_value' => $variableValue,
);
}
}
$form['variables']['rebuildArgs'] = [
'#type' => 'submit',
'#value' => t('Rebuild Arguments'),
'#submit' => array(
'::rebuildMessageArguments',
),
'#ajax' => [
'callback' => '::rebuildMessageArguments',
'wrapper' => 'Variables-fieldset-wrapper',
],
];
$form['tokens'] = array(
'#prefix' => '<div id="token-tree"></div>',
'#markup' => $this->tokenTree,
);
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $heartbeat_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\heartbeat\\Entity\\HeartbeatType::load',
],
'#disabled' => !$heartbeat_type
->isNew(),
];
$form_state
->setCached(FALSE);
return parent::form($form, $form_state, $heartbeat_type);
}
public function save(array $form, FormStateInterface $form_state) {
$heartbeat_type = $this->entity;
$heartbeat_type
->set('description', $form_state
->getValue('description'));
$heartbeat_type
->set('message', $form_state
->getValue('message'));
$heartbeat_type
->set('perms', $form_state
->getValue('perms'));
$heartbeat_type
->set('variables', $form_state
->getValue('variables'));
$heartbeat_type
->set('arguments', json_encode($form_state
->get('messageMap')));
$heartbeat_type
->set('mainentity', $this->entityTypes[$form_state
->getValue('entity_type')]);
$heartbeat_type
->set('bundle', $form_state
->get('entity_bundles')[$form_state
->getValue('entity_bundles')['list']]);
$status = $heartbeat_type
->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Heartbeat type.', [
'%label' => $heartbeat_type
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Heartbeat type.', [
'%label' => $heartbeat_type
->label(),
]));
}
$form_state
->setRedirectUrl($heartbeat_type
->toUrl('collection'));
}
public function rebuildMessageArguments(array &$form, FormStateInterface $form_state) {
$messageArgString = $form_state
->getValue('message');
if ($form_state != NULL) {
$argsArray = $this
->extractMessageArguments($messageArgString, $form_state);
foreach ($argsArray as $key => $arg) {
$this->messageMap[$key] = '!' . $arg;
}
$form_state
->set('messageMapKey', $this->messageMap);
$form_state
->set('data_hidden', $argsArray);
$form_state
->setRebuild();
return $form['variables'];
}
else {
return NULL;
}
}
public function prepareVariables(&$form, FormStateInterface $form_state) {
return $form['variables'];
}
private function extractMessageArguments($message) {
$messageArguments = array_slice(explode('!', $message), 1);
$argsArray = array();
foreach ($messageArguments as $argument) {
if (strlen($argument) > 0) {
$cleanArgument = strpos($argument, ' ') ? substr($argument, 0, strpos($argument, ' ')) : $argument;
$argsArray[] = $cleanArgument;
$this->messageMap[] = '!' . $cleanArgument;
}
}
return $argsArray;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$messageMapKeysget = $form_state
->get('messageMapKey');
if ($variables = $form_state
->getValue('variables')) {
$num = count($variables);
for ($i = 0; $i < $num; $i++) {
if (!is_string($variables[$i])) {
continue;
}
$this->messageMap[$messageMapKeysget[$i]] = $variables[$i];
}
$form_state
->set('messageMap', $this->messageMap);
parent::submitForm($form, $form_state);
}
}
public function getBundlesForEntity(array &$form, FormStateInterface $form_state) {
$entityType = $this->entityTypes[$form_state
->getValue('entity_type')];
$entity = $this->entityTypeManager
->getStorage($entityType);
$bundleTypeName = $entity
->getEntityType()
->getBundleEntityType();
$bundles = $this->entityTypeManager
->getStorage($bundleTypeName)
->loadMultiple();
$bundleNames = array();
foreach ($bundles as $bundle) {
$bundleNames[] = $bundle
->id();
}
$form_state
->set('entity_bundles', $bundleNames);
$form_state
->setRebuild();
return $form['entity_bundles'];
}
public function tokenSelectDialog(array &$form, FormStateInterface $form_state) {
$ajax_response = new AjaxResponse();
$ajax_response
->addCommand(new InvokeCommand('.token-tree', 'css', array(
'display',
'block',
)));
$this->treeAdded = TRUE;
return $ajax_response;
}
public function getBundlesAlternate(array &$form, FormStateInterface $form_state) {
$ajax_response = new AjaxResponse();
$AddChartForm = \Drupal::formBuilder()
->getForm('Drupal\\heartbeat\\Form\\HeartbeatTypeForm');
$ajax_response
->addCommand(new HtmlCommand('#formarea', $AddChartForm));
}
}