You are here

function entityconnect_add in Entity connect 7

Same name and namespace in other branches
  1. 7.2 includes/entityconnect.menu.inc \entityconnect_add()

Page callback: Load cached form info.

This page will load the cached form info, and display links to each of the entityreference types. If there is only one it will redirect to that page.

This is mostly a copy and hack up of the node add page.

This page is directed to from the entityconnect_add_button_submit.

1 string reference to 'entityconnect_add'
entityconnect_menu in ./entityconnect.module
Implements hook_menu().

File

includes/entityconnect.menu.inc, line 41
Handles all entityconnect menu item page callbacks.

Code

function entityconnect_add($cache_id) {
  $content = array();
  $cache = entityconnect_cache_get($cache_id);
  $field = field_info_field($cache->data['field']);
  $entity_type = $cache->data['target_entity_type'];
  $acceptable_types = $cache->data['acceptable_types'];
  switch ($entity_type) {
    case 'node':
      if (count($acceptable_types) > 0) {
        foreach (node_type_get_types() as $key => $item) {
          if (isset($acceptable_types[$key]) && $acceptable_types[$key]) {
            $type = str_replace("_", '-', $key);
            $content[$key] = array(
              'href' => "node/add/{$type}/{$cache_id}",
              'label' => $item->name,
              'description' => $item->description,
            );
          }
        }

        // If we have only one content-type defined,
        // go directly to the node form.
        if (sizeof($content) == 1) {
          $item = array_pop($content);
          drupal_goto($item['href']);
        }
      }
      else {
        foreach (node_type_get_types() as $key => $item) {
          $type = str_replace("_", '-', $key);
          $content[$key] = array(
            'href' => "node/add/{$type}/{$cache_id}",
            'label' => $item->name,
            'description' => $item->description,
          );
        }
      }
      $output = theme('entityconnect_entity_add_list', array(
        'cache id' => $cache_id,
        'items' => $content,
      ));
      $output .= l(t('Cancel'), "admin/entityconnect/return/{$cache_id}");
      break;
    case 'user':
      $item['href'] = "admin/people/create/{$cache_id}";
      drupal_goto($item['href']);
      break;
    case 'taxonomy_term':
      $voc_list = array();
      if (count($acceptable_types) > 0) {
        foreach (taxonomy_get_vocabularies() as $key => $item) {
          $type = $item->machine_name;
          if (isset($acceptable_types[$type]) && $acceptable_types[$type]) {
            $item->href = "admin/structure/taxonomy/{$type}/add/{$cache_id}";
            $content[$key] = $item;
            $voc_list[] = l($item->name, $item->href);
          }
        }

        // If we have only one vocabulary defined,
        // go directly to the term add form.
        if (sizeof($content) == 1) {
          $item = array_pop($content);
          drupal_goto($item->href);
        }
      }
      else {
        foreach (taxonomy_get_vocabularies() as $key => $item) {
          !isset($item->href) ? $item->href = NULL : $item->href;
          $type = $item->machine_name;
          $item->href = "admin/structure/taxonomy/{$type}/add/{$cache_id}";
          $content[$key] = $item;
          $voc_list[] = l($item->name, $item->href);
        }
      }
      $output = theme('entityconnect_taxonomy_term_add_list', array(
        'items' => $content,
      ));
      $output .= l(t('Cancel'), "admin/entityconnect/return/{$cache_id}");
      break;
    case 'taxonomy_vocabulary':
      $item['href'] = "admin/structure/taxonomy/add/{$cache_id}";
      drupal_goto($item['href']);
      break;
    default:

      // Entity construction kit support.
      if (module_exists('eck')) {
        $entity_info = entity_get_info($entity_type);
        if (!empty($entity_info['module']) && $entity_info['module'] == 'eck') {

          // Only one allowed, no need for the intermediate step.
          if (count($acceptable_types) === 1) {
            $bundle_name = array_pop($acceptable_types);
            $bundle = $entity_info['bundles'][$bundle_name];
            drupal_goto($bundle['crud']['add']['path'] . "/{$cache_id}");
          }
          elseif (count($acceptable_types) === 0) {
            foreach ($entity_info['bundles'] as $key => $bundle) {
              $content[$key]['href'] = $bundle['crud']['add']['path'] . "/{$cache_id}";
              $content[$key]['label'] = $bundle['label'];
            }
          }
          else {
            foreach ($acceptable_types as $bundle_name) {
              $bundle = $entity_info['bundles'][$bundle_name];
              $content[$bundle_name] = array(
                'href' => $bundle['crud']['add']['path'] . "/{$cache_id}",
                'label' => $bundle['label'],
                'description' => '',
              );
            }
          }
          $output = theme('entityconnect_entity_add_list', array(
            'cache id' => $cache_id,
            'items' => $content,
          ));
          $output .= l(t('Cancel'), "admin/entityconnect/return/{$cache_id}");
        }
      }

      // Bean support.
      if (module_exists('bean_admin_ui')) {
        if (count($acceptable_types) > 0) {
          foreach (bean_admin_ui_get_types() as $key => $item) {
            if (isset($acceptable_types[$key]) && $acceptable_types[$key]) {
              $type = str_replace("_", '-', $key);
              $content[$key] = array(
                'href' => "block/add/{$type}/{$cache_id}",
                'label' => $item->label,
                'description' => $item->description,
              );
            }
          }

          // If we have only one content-type defined,
          // go directly to the node form.
          if (sizeof($content) == 1) {
            $item = array_pop($content);
            drupal_goto($item['href']);
          }
        }
        else {
          foreach (bean_admin_ui_get_types() as $key => $item) {
            $type = str_replace("_", '-', $key);
            $content[$key] = array(
              'href' => "block/add/{$type}/{$cache_id}",
              'label' => $item->label,
              'description' => $item->description,
            );
          }
        }
        $output = theme('entityconnect_entity_add_list', array(
          'cache id' => $cache_id,
          'items' => $content,
        ));
        $output .= l(t('Cancel'), "admin/entityconnect/return/{$cache_id}");
      }
      break;
  }
  return $output;
}