从用户位置继承节点位置

时间:2012-09-11 11:40:57

标签: drupal location

我想要做的是使用Drupal 7,而Location模块是将节点位置继承或默认为用户位置。原因是我只希望用户编辑一个位置(在他们的个人资料上)并在该用户创建的所有节点上显示。有人可以帮忙吗?

http://drupal.org/node/1357614

http://drupal.org/node/377600

编辑:

我找到了这个模块,我试图让Latitide和Longitude工作(正如其他人一样),但是父案例'locpick'让我感到困惑,我无法理解这一点。

<?php

function location_alter($argument){
$location_alter = array();
global $user;
$uid = $user->uid;
$location_alter = user_load($uid);
$location_alter = $location_alter->location;
return $location_alter[$argument];
}

/**
* Implementation of hook_locationapi().
*/
function locnodeauthor_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
switch ($op) {
 case 'field_expand':
  switch ($a3) {
    case 'name':
      return array(
        '#type'           => 'textfield',
        '#title'          => t('Location name'),
        '#default_value'  => $obj != '' ? $obj : location_alter('name'),
        '#size'           => 64,
        '#maxlength'      => 64,
        '#description'    => t('e.g. a place of business, venue, meeting point'),
        '#attributes'     => NULL,
        '#required'       => ($a4 == 2),
      );

    case 'street':
      return array(
        '#type'           => 'textfield',
        '#title'          => t('Street'),
        '#default_value'  => $obj != '' ? $obj : location_alter('street'),
        '#size'           => 64,
        '#maxlength'      => 64,
        '#required'       => ($a4 == 2),
      );

    // Additional is linked to street.
    case 'additional':
      return array(
        '#type'           => 'textfield',
        '#title'          => t('Additional'),
        '#default_value'  => $obj != '' ? $obj : location_alter('additional'),
        '#size'           => 64,
        '#maxlength'      => 64,
        // Required is forced OFF because this is technically part of street.
      );

    case 'city':
      return array(
        '#type'           => 'textfield',
        '#title'          => t('City'),
        '#default_value'  => $obj != '' ? $obj : location_alter('city'),
        '#size'           => 64,
        '#maxlength'      => 64,
        '#description'    => NULL,
        '#attributes'     => NULL,
        '#required'       => ($a4 == 2),
      );

    case 'province':
      drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js');
      $country = $a5['country'] ? $a5['country'] : variable_get('location_default_country', 'us');
      return array(
        '#type' => 'textfield',
        '#title' => t('State/Province'),
        '#autocomplete_path' => 'location/autocomplete/'. $country,
        '#default_value'  => $obj != '' ? $obj : location_alter('province'),
        '#size' => 64,
        '#maxlength' => 64,
        '#description' => NULL,
        // Used by province autocompletion js.
        '#attributes' => array('class' => array('location_auto_province')),
        '#required' => ($a4 == 2),
      );

    case 'country':
      // Force default.
      if ($a4 == 4) {
        return array(
          '#type' => 'value',
          '#value' => variable_get('location_default_country', 'us'),
        );
      }
      else {
        $options = array_merge(array('' => t('Please select'), 'xx' => 'NOT LISTED'), location_get_iso3166_list());
        return array(
          '#type'           => 'select',
          '#title'          => t('Country'),
          '#default_value'  => $obj,
          '#options'        => $options,
          '#description'    => NULL,
          '#required'       => ($a4 == 2),
          // Used by province autocompletion js.
          '#attributes'     => array('class' => array('location_auto_country')),
        );
      }
      break;

    case 'postal_code':
      return array(
        '#type'           => 'textfield',
        '#title'          => t('Postal code'),
        '#default_value'  => $obj != '' ? $obj : location_alter('postal_code'),//$location_alter['postal_code'],//location_alter('postal_code'),
        '#size'           => 16,
        '#maxlength'      => 16,
        '#required'       => ($a4 == 2),
      );
  }
  break;


case 'isunchanged':
  switch ($a3) {
    case 'lid':
      // Consider 0, NULL, and FALSE to be equivilent.
      if (empty($obj[$a3]) && empty($a4)) {
        return TRUE;
      }
      break;

    case 'country':
      // Consider '  ' and '' to be equivilent, due to us storing country
      // as char(2) in the database.
      if (trim($obj[$a3]) == trim($a4)) {
        return TRUE;
      }
      break;

    case 'province_name':
    case 'country_name':
    case 'map_link':
    case 'coords':
      // Always considered unchanged.
      return TRUE;
  }
  break;

}

}

1 个答案:

答案 0 :(得分:0)

根据您打算如何使用此结果,您甚至可能不需要保存节点的位置。即,如果要访问视图中的位置数据,则可以使用关系来访问节点作者的位置数据,即使位置数据可能根本不保存在节点上。但是,这样做意味着视图中的节点将始终与作者的当前位置相关联,而不是他们在创建节点时设置的位置。

如果您确实要将其保存在节点上,则需要创建自定义模块。您可能希望调用hook_form_alter隐藏节点编辑表单上的位置字段,并hook_node_presave将用户的位置值复制到节点的位置值。