You are here

function entityform_entity_presave in Entityform 7

Same name and namespace in other branches
  1. 7.2 entityform.module \entityform_entity_presave()

Implements hook_entity_presave().

Manage path aliases for Entityform Types

File

./entityform.module, line 807
Module for the Entityform Entity - a starting point to create your own Entity and associated administration interface

Code

function entityform_entity_presave($entity, $type) {
  if ($type == 'entityform_type') {
    if (module_exists('path')) {
      if (isset($entity->paths)) {
        $original_entity = $entity->original;
        foreach ($entity->paths as $key => $path) {
          $path = $entity->paths[$key];
          $path['alias'] = trim($path['alias']);

          // Delete old alias if user erased or changed it.
          if (isset($original_entity->paths[$key])) {
            $original_path = $original_entity->paths[$key];
            if (!empty($original_path['alias']) && empty($path['alias']) || $path['alias'] != $original_path['alias']) {
              path_delete($original_path);
            }
          }
          if (!empty($path['alias'])) {

            // Ensure fields for programmatic executions.
            switch ($key) {
              case 'submit':
                $path['source'] = _entityform_type_get_submit_url($entity->type);
                break;
              case 'confirm':
                $path['source'] = _entityform_type_get_confirm_url($entity->type);
                break;
            }

            // Entityform Types aren't translateable
            // Check to see if alias is exact same as original
            if (!empty($original_path['alias']) && $original_path['alias'] == $path['alias'] && $path['source'] == $original_path['source']) {
              continue;
            }
            $path['language'] = LANGUAGE_NONE;
            if (_entityform_alias_is_used($path)) {
              watchdog('entityform', "Could not create alias @alias for EntityformType entity '@type'", array(
                '@alias' => $path['alias'],
                '@type' => $entity->type,
              ), WATCHDOG_WARNING);
            }
            else {
              path_save($path);
            }
          }
        }
      }
    }
    if (module_exists('menu')) {
      if (isset($entity->menu)) {
        $link =& $entity->menu;
        if ($link['enabled']) {
          $link = $entity->menu;
          $link['link_path'] = _entityform_type_get_submit_url($entity->type);
          list($link['menu_name'], $link['plid']) = explode(':', $link['parent']);
          if (trim($link['description'])) {
            $link['options']['attributes']['title'] = trim($link['description']);
          }
          else {

            // If the description field was left empty, remove the title attribute
            // from the menu link.
            unset($link['options']['attributes']['title']);
          }
          if (!menu_link_save($link)) {
            drupal_set_message(t('There was an error saving the menu link.'), 'error');
          }
        }
        else {
          if (!empty($link['mlid'])) {
            menu_link_delete($link['mlid']);
          }
        }
      }
    }
  }
}