View source
<?php
namespace Drupal\gathercontent_ui\Form;
use Cheppers\GatherContent\GatherContentClientInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\gathercontent\DrupalGatherContentClient;
use Drupal\gathercontent\Entity\Mapping;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MappingImportForm extends EntityForm {
use StringTranslationTrait;
protected $client;
public function __construct(GatherContentClientInterface $client) {
$this->client = $client;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('gathercontent.client'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$account_id = DrupalGatherContentClient::getAccountId();
$projects = [];
if ($account_id) {
$projects = $this->client
->getActiveProjects($account_id);
}
$form['description'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t("Please select the GatherContent Templates you'd like to map. Only Templates you've not selected will be listed."),
'#attributes' => [
'class' => [
'description',
],
],
];
$form['projects'] = [
'#type' => 'vertical_tabs',
];
$form['template_counter'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'gather-content-counter-message',
],
],
'#attached' => [
'library' => [
'gathercontent_ui/template_counter',
],
],
];
$created_mapping_ids = Mapping::loadMultiple();
$local_templates = [];
foreach ($created_mapping_ids as $mapping) {
$local_templates[$mapping
->getGathercontentTemplateId()] = $mapping
->getGathercontentTemplate();
}
foreach ($projects['data'] as $project) {
$remote_templates = $this->client
->getTemplatesOptionArray($project->id);
$templates = array_diff_assoc($remote_templates, $local_templates);
if (empty($templates)) {
continue;
}
$form['p' . $project->id] = [
'#type' => 'details',
'#title' => $project->name,
'#group' => 'projects',
'#tree' => TRUE,
];
$form['p' . $project->id]['templates'] = [
'#type' => 'checkboxes',
'#title' => $project->name,
'#options' => $templates,
'#attributes' => [
'class' => [
'gather-content-counted',
],
],
];
}
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#id'] == 'edit-submit') {
$account_id = DrupalGatherContentClient::getAccountId();
if (!$account_id) {
$this
->messenger()
->addError($this
->t('No available accounts.'));
$form_state
->setRedirect('entity.gathercontent_mapping.collection');
return;
}
$projects = $this->client
->getActiveProjects($account_id);
$values = $form_state
->getValues();
foreach ($values as $k => $tree) {
if (!is_array($tree)) {
continue;
}
$templates = array_filter($values[$k]['templates']);
foreach ($templates as $template_id => $selected) {
$template = $this->client
->templateGet($template_id);
$templateBody = $this->client
->getBody(TRUE);
$mapping_values = [
'id' => $template_id,
'gathercontent_project_id' => $template['data']->projectId,
'gathercontent_project' => $projects['data'][$template['data']->projectId]->name,
'gathercontent_template_id' => $template_id,
'gathercontent_template' => $template['data']->name,
'template' => serialize($templateBody),
];
$mapping = $this->entityTypeManager
->getStorage('gathercontent_mapping')
->create($mapping_values);
if (is_object($mapping)) {
$mapping
->save();
}
}
}
}
$form_state
->setRedirect('entity.gathercontent_mapping.collection');
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Select');
$actions['close'] = [
'#type' => 'submit',
'#value' => $this
->t('Close'),
];
return $actions;
}
}