entityconnect-8.x-2.0-rc1/entityconnect.module
entityconnect.module
<?php
/**
* @file
* Contains entityconnect.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\entityconnect\Controller\EntityconnectController;
use Drupal\entityconnect\EntityconnectFormUtils;
/**
* Implements hook_help().
*/
function entityconnect_help($route_name, RouteMatchInterface $route_match) {
$module_path = \Drupal::service('extension.list.module')->getPath('entityconnect');
switch ($route_name) {
// Main module help for the entityconnect module.
case 'help.page.entityconnect':
$output = '';
$filepath = $module_path . '/README.md';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
}
if (!isset($readme)) {
return NULL;
}
if (\Drupal::moduleHandler()->moduleExists('markdown')) {
$filters = \Drupal::moduleHandler()->invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
}
else {
$output = '<pre>' . $readme . '</pre>';
}
}
else {
$output = '<pre>' . $readme . '</pre>';
}
return $output;
}
}
/**
* Implements hook_module_implements_alter().
*/
function entityconnect_module_implements_alter(&$implementations, $hook) {
if ('form_alter' == $hook && isset($implementations['entityconnect'])) {
$group = $implementations['entityconnect'];
unset($implementations['entityconnect']);
$implementations['entityconnect'] = $group;
}
}
/**
* Implements hook_theme().
*/
function entityconnect_theme() {
$theme = [
// Entityconnect Entity add list page.
'entityconnect_entity_add_list' => [
'variables' => [
'items' => NULL,
'cache_id' => NULL,
'cancel_link' => NULL,
],
'file' => 'templates/entityconnect.theme.inc',
],
];
return $theme;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function entityconnect_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
EntityconnectFormUtils::fieldConfigEditFormAlter($form, $form_state);
}
/**
* Implements hook_form_alter().
*/
function entityconnect_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Get the cache from the id and set the form state if this is a child form.
$request = \Drupal::request();
if (!empty($request->query->get('build_cache_id'))) {
$cid = $request->query->get('build_cache_id');
$cache_data = \Drupal::getContainer()->get('entityconnect.cache')->get($cid);
if (!empty($request->query->get('child'))) {
$form_state->set('#entityconnect_child_form', [$cid => $cache_data]);
}
if (!empty($request->query->get('return')) && !empty($cache_data) && $cache_data['form']['#form_id'] == $form_id) {
$request->query->remove('build_cache_id');
EntityconnectFormUtils::returnFormAlter($form, $form_state, $cache_data);
\Drupal::getContainer()->get('entityconnect.cache')->delete($cid);
}
}
// If this form is a child form let's add alter for that purpose
// Note that we are doing this here because when we return to a form it gets
// rebuilt so this will get caught in the rebuilt.
if ($cache = $form_state->get('#entityconnect_child_form')) {
EntityconnectFormUtils::childFormAlter($form, $form_state, $form_id, key($cache));
}
// Adds entity connect buttons to any ref fields in the form.
EntityconnectFormUtils::entityFormAlter($form, $form_state);
if (isset($form['actions']['save_edit']) &&
$key = array_search('save_edit_form_submit_redirect', $form['actions']['save_edit']['#submit'])
) {
// Fix an issue with removed query parameters.
unset($form['actions']['save_edit']['#submit'][$key]);
$form['actions']['save_edit']['#submit'][] = 'entityconnect_save_edit_submit_redirect';
}
}
/**
* Alter the redirect from the 'save & edit' module.
*/
function entityconnect_save_edit_submit_redirect(&$form, FormStateInterface $form_state) {
$entity = $form_state->getFormObject()->getEntity();
/** @var \Drupal\Core\Url $url */
$url = $entity->toUrl('edit-form');
if ($query = \Drupal::request()->query->all()) {
$url->setOption('query', $query);
}
if (!empty($query['destination'])) {
$url->setRouteParameter('destination', $query['destination']);
}
\Drupal::request()->query->remove('destination');
$form_state->setRedirectUrl($url);
}
/**
* Implements hook_entityconnect_add_info().
*/
function entityconnect_entityconnect_add_info($cache_id, $entity_type, $acceptable_types) {
return EntityconnectController::addInfo($cache_id, $entity_type, $acceptable_types);
}
/**
* Implements hook_entityconnect_edit_info().
*/
function entityconnect_entityconnect_edit_info($cache_id, $entity_type, $target_id) {
return EntityconnectController::editInfo($cache_id, $entity_type, $target_id);
}
