You are here

entityconnect.module in Entity connect 8.2

Same filename and directory in other branches
  1. 7.2 entityconnect.module
  2. 7 entityconnect.module

File

entityconnect.module
View source
<?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_get_path('module', '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;
    default:
  }
}

/**
 * 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() for field_config_edit_form.
 */
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);
}

/**
 * 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);
}