Is it currently possible to hide the Path and Pathauto fields on e.g. a node form for certain content types while still allowing the alias to be created? I've tried doing a '#pre_render' callback and then change some of the $element['path'] values, but no matter what I do, if the fields end up being hidden then no alias is created, it just ends up as node/123. Any tips?

Comments

joekrukosky’s picture

Issue summary: View changes

I am running into this same issue. I have 'hidden' the node form path element for certain users in an after_build with:
$form['path']['#access'] = FALSE;

But for those users it disables the automatic alias generation. I have attempted to keep it enabled by also adding:
$form['#node']->path['pathauto'] = TRUE;

This doesn't work.

Anyone have any suggestions?

joekrukosky’s picture

Here's a solution I came up with. Not sure it's the best way, but it worked for my application.

/**
 * Implements hook_node_insert().
 */
function mymodule_node_insert($node) {
  global $user;
  // Pathauto gets disabled when form element is turned off, so enable it.
  // Do this only for a particular content type (page) and skip for admin. 
  if ($node->type == 'page' && $user->uid != 1) {
    $node->path['pathauto'] = TRUE;
  }
}

/**
 * Implements hook_node_update().
 */
function mymodule_node_update($node) {
  global $user;
  // Pathauto gets disabled when form element is turned off, so enable it.
  // Do this only for a particular content type (page) and skip for admin. 
  if ($node->type == 'page' && $user->uid != 1) {
    $node->path['pathauto'] = TRUE;
  }
}
adam-delaney’s picture

I believe by setting the #access to FALSE the pathauto default value is not getting set so a path is not being derived, however if you explicitly set the pathauto value to TRUE while setting the path access to FALSE you may hide the form items while still setting an automatic alias. This code worked for me.

/**
* Implements hook_form_node_form_alter().
*/
function hook_form_node_form_alter(&$form, &$form_state) {
  // Disable url path access for specific nodes
  if (!user_access('administer url aliases')) {
    $exceptions = array('page', 'administrative_unit');
    if (!in_array($type, $exceptions)) {
      $form['#after_build'][] = 'node_form_overrides_url_path_access_restriction';
    }
  }
}

// Custom functions to set path access
function node_form_overrides_url_path_access_restriction($form, &$form_state) {
  $form['path']['#access'] = FALSE;
  $form_state['values']['path']['pathauto'] = 1;
  return ($form);
}