zoukankan      html  css  js  c++  java
  • drupal7 学习笔记(持续更新中...)

    最近折腾了一下drupal7,大概的了解了一下,头昏那个脑涨啊。找网上的介绍倒是一大堆,但大多是e文的,中文的一半都是一知半解的叙述。 与痛苦和纠结中总结了一些经验,几记录下来备用。具体如下:

    一  安装相关

      1 安装drupal超时(主要是安装中文翻译的时候)

      方法一:
      修改php.ini文件:memory_limit = 256M (依实际情况设定)
      方法二:
      打开\sites\default\settings.php文件,在最后增加以下两行:
      ini_set(‘memory_limit’, ’256M’); //加大php的内存 也可以在php.ini中设置
      ini_set(‘max_execution_time’, 2000); //加大页面执行时间 php.ini中的默认值是30 (秒)

    二  主题相关

      1 主题出问题后强制恢复系统默认主题

    UPDATE system SET status = 0 WHERE type = 'theme'; 
    UPDATE system SET status=1 WHERE type='theme' AND (name = 'seven' OR name = 'bartik'); 
    TRUNCATE cache; 
    TRUNCATE cache_block; 
    TRUNCATE cache_bootstrap; 
    TRUNCATE cache_field; 
    TRUNCATE cache_filter; 
    TRUNCATE cache_form; 
    TRUNCATE cache_image; 
    TRUNCATE cache_menu; 
    TRUNCATE cache_path; 
    TRUNCATE cache_page; 
    TRUNCATE cache_update; 
    TRUNCATE cache_views; 
    TRUNCATE cache_views_data;

      2 获取当前页的模版文件列表

      在你使用的当前主题文件夹下有个template.php,在其中的xxxxx_process_page方法代码块中(没有就建立)增加一下代码:

    var_dump($variables['theme_hook_suggestions']);

      这样你随便打开某个页面就会在页首打印出该页获取的模版文件的顺序以及模版文件名称,一目了然,注意系统加载的优先级顺序是倒序的。而不用去翻n多文档,实验n次,纠结n次了。。。,明眼人可能看出来了,变量列表中的theme_hook_suggestions就是模版信息数组。你其实可以随便修改定制的。。。呃,慎重,最好按照后面一个小结的方法来增加。

      3 自定义模板文件名称

      有时候你想根据特定的格式来获取模版文件。举个例子,如果你想根据内容类型来制定模版,那么你可以同样在template.php中xxxxx_process_page方法中增加一下代码

     //增加模版选择器
    if (!empty($variables['node'])) {
        $node = $variables['node'];
        $variables['theme_hook_suggestions'][] = 'page__type__' . $node->type;
    }

      这样当drupal打开某内容的页面时,将优先使用你定义的模版文件,是不是貌似很吊?当然更改之后记得清空系统自己的缓存,要不然它依然会加载之前默认的模板,只需执行一下:

    delete from cache

      

      另外值得一提的是HOOK的复写,上面的xxxxx_process_page其实就是 hook_process_HOOK的复写,而drupal加载这些HOOK的顺序如下:

         template_preprocess()
            template_preprocess_hello()
            helloModule_preprocess()
            helloModule_preprocess_hello()
            phptemplate_preprocess()
            phptemplate_preprocess_hello()
            helloTheme_preprocess()
            helloTheme_preprocess_hello()
            template_process()
      所以你可以自己斟酌在哪里复写,当然你也可以集中管理,例如我的项目中我是这么写的:
    function diantang_process(&$variables,$hook){
      $node = $variables['node'];
      switch ($hook) {
        case 'comment':
          $variables['theme_hook_suggestions'][]='comment__'.$node->type;
          break;
        case 'page':
          if (!empty($variables['node'])){
            $variables['theme_hook_suggestions'][] = 'page__type__' . $node->type;
          }
        break;
        default:
          break;
      }
    }

      4 动态改变当前页面所对应的菜单项

      依然是在xxxxx_process_page中增加以下代码:

    if (!empty($variables['node'])) {
        $node = $variables['node'];
        //所有的 'selftype' 类型的node的menu设置成为其对应的tag的菜单项
        switch ($variables['node']->type) {
          case 'selftype':
            $tid=$variables['node']->field_tags_news['und'][0]['tid'];
            menu_set_active_item('taxonomy/term/'.$tid);
          break;
        }
      }

      这样,如果当前页面的内容类型是selftype的话,那么当前激活的菜单就将是该内容类型对应的标签。其中menu_set_active_item中设置的是 菜单项的系统地址。

    三 一些常用的方法

      1 获取,node中自定义的字段
      $node= node_load(12);
      $items = field_get_items('node', $node, 'field_image');
      2 获取node中自定义字段的可以render显示的数组:
      $node= node_load(12);
      $items = field_get_items('node', $node, 'field_image');
      $output = field_view_value('node', $node, 'field_image', $items[0]);
      print render($output);
      3 获取node中某文件字段(图片,媒体)的地址
      $node= node_load(12);
      $items = field_get_items('node', $node, 'field_image');
      $url= file_create_url($items[0]["uri"])
      4 drupal 中现在已经内置了jquery和jqueryui模块,但是jquery至增加了core,比如我们要加一个tabs;
      drupal_add_library('system', 'ui.tabs');
      drupal_add_js('jQuery(document).ready(function(){jQuery( "#mytabs" ).tabs();});', 'inline');

      5 drupal中常用的模块api
      // node
      node_load($nid = NULL, $vid = NULL, $reset = FALSE);
      node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE);

      // user
      user_load($uid, $reset = FALSE);
      user_load_multiple($uids = array(), $conditions = array(), $reset = FALSE);

      // menu tree
      menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL);
      menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE);

      // term
      taxonomy_term_load($tid) : object
      taxonomy_term_load_multiple($tids = array(), $conditions = array()) : array
      taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) : array

      // block
      block_load($module, $delta);

    四  一些数据库操作

           // select
        db_select('node', 'n')
            ->extend('PagerDefault')->limit(5)
            ->fields('n');
        $statement->fetchField();
        
        db_query_range('SELECT n.nid, n.title, n.created
          FROM {node} n WHERE n.uid = :uid', 0, 10, array(':uid' => $uid));
    
        // insert
        $fields = array('nid' => 1, 'title' => 'my title', 'body' => 'my body');
        db_insert('node')->fields($fields)->execute();
    
        // update
        db_update('example')
          ->condition('id', $id)
          ->fields(array('field2' => 10))
          ->execute();
    
        // select
        $query = db_select('comment', 'c')
          ->fields('c', array('subject', 'name'))
          ->fields('n', array('title'))
          ->extend('PagerDefault')->limit(5)
          ->condition('n.type', array('article'), 'IN')
          ->orderBy('c.cid', 'DESC');
        $query->join('node', 'n', 'n.nid = c.nid');
        $statement = $query->execute();
    
        $query = db_select('node', 'n')->fields('n', array('title'))->distinct();
        $query->join('taxonomy_index', 't', 't.nid = n.nid');
        $or = db_or()->condition('n.uid', $authorId)->condition('t.tid', $cats, 'IN');
        $query->condition($or)->execute();
    
        // fetch
        foreach ($query->execute() as $object) {
          echo $object->name;
        }    
  • 相关阅读:
    转 linux 内存释放
    转 功能强大的shell:if条件语句
    转 shell中$(( )) 与 $( ) 还有${ }的区别
    转 关于shell中if 语法结构的广泛误解
    配置vim插件遇到youcompleteme插件问题解决方案
    lucene关于IndexReader总结
    sql developer链接不上oracle 报 The Network Adapter could not establish the connection
    plsql数据库异常---plsql 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致
    改修jquery支持cmd规范的seajs
    npm学习(十六)之package-lock.json和package.json的作用
  • 原文地址:https://www.cnblogs.com/cczw/p/2727488.html
Copyright © 2011-2022 走看看