【问题标题】:How To Create New Content Type Programmatically in Drupal 7?如何在 Drupal 7 中以编程方式创建新的内容类型?
【发布时间】:2011-12-28 10:50:41
【问题描述】:

我正在 Drupal 7 中构建一个模块 (my_module)。

它有一些功能,也会创建新的内容类型。

my_module.install 我实现了hook_install (my_module_install)。

我可以使用hook_install 的多个实现在此模块中创建新的内容类型(my_cck_install)吗?

如果(是),我应该怎么做?

其他:我是否在另一个模块中执行此操作? :-)

【问题讨论】:

    标签: drupal module drupal-7 content-type


    【解决方案1】:

    你不能在同一个模块中使用多个hook_install 的实现;在 PHP 中,你不能有 2 个同名的函数来排除这种情况。

    无论如何,您只需要在相同的hook_install 中添加您的新内容类型(查看/profiles/standard/standard.install 中的标准安装配置文件是如何做到的)。这就是我总是从安装文件中添加新内容类型的方式(使用推荐模块的示例):

    function testimonial_install() {
      // Make sure a testimonial content type doesn't already exist
      if (!in_array('testimonial', node_type_get_names())) {
        $type = array(
          'type' => 'testimonial',
          'name' => st('Testimonial'),
          'base' => 'node_content',
          'custom' => 1,
          'modified' => 1,
          'locked' => 0,
          'title_label' => 'Customer / Client Name'
        );
    
        $type = node_type_set_defaults($type);
        node_type_save($type);
        node_add_body_field($type);
      }
    }
    

    【讨论】:

    • 我只在数据库中看到“推荐”,我要添加什么才能在管理/结构/类型中看到“推荐”?
    • 清除 Drupal 的缓存,应该这样做。尝试直接访问admin/structure/types/testimonial 以确保您的代码已实际运行
    • 我很抱歉我的错误,它根本不起作用。我不应该使用 hook_node_info() 吗?
    • 这完全取决于。如果您想自己处理内容类型的功能,那么是的,您想创建所谓的“节点模块”(使用hook_node_info() 等)。如果您只是想创建一个没有特殊处理的内容类型,那么上面的方法要容易得多。放入新代码时是否确保卸载并重新安装模块?
    • 不,我还没有。你能给我一个方向吗?请。
    【解决方案2】:

    以下代码将创建一个名为“事件”的内容类型,其机器名称为“事件”和标题字段 -

    //CREATE NEW CONTENT TYPE
    function orderform_node_info() {
      return array(
        'event' => array(
        'name' => t('Event'),
        'base' => 'event',
        'description' => t('A event content type'),
        'has_title' => TRUE
        ),
      );
    }
    
    
    function event_form($node,$form_state) {
      $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('event Title'),
        '#default_value' => !empty($node->title) ? $node->title : '',
        '#required' => TRUE,
        '#weight' => -5
      );
      return $form;
    }
    //END CONTENT TYPE
    

    您应该将它放在您的.module 文件中...如果您想向其中添加其他字段,请告诉我,我会用代码修补您...祝您好运!

    【讨论】:

    【解决方案3】:
         /**
         * Implements hook_node_info()
         */
        function mymodule_node_info() {
            return array(
                'news' => array(
                    'name' => t('News'),
                    'base' => 'news',
                    'description' => t('You can add  News here'),
                    'has_title' => TRUE,
                    'title_label' => t('News title')
                 )
            );
        }
    
     /**
     * Implement hook_form()
     */
    function mymodule_form($node, $form_state) {
        return node_content_form($node, $form_state);
    }
    

    在mymodule.install中添加实现如下:

    /**
     * Implements hook_install().
     */
    function mymodule_install() {
        node_types_rebuild();
        $types = node_type_get_types();|
          node_add_body_field($types['news']);
    }
    

    您可以从here获取详细的代码描述

    【讨论】:

      【解决方案4】:

      /* * .Module 文件中的钩子节点信息实现 */

      function test_node_info() {
      return array(
        'product' => array(
          'name' => t('Product'),
          'base' => 'product',
          'description' => t('Product Title'),
        )
      );
      

      }

      /** * 实现 hook_form() */

      function product_form($node, $form_state) {
      return node_content_form($node, $form_state);
      

      } /** * 在 .install 文件 中实现 hook_install()。 */

      function test_install() {
      node_types_rebuild();
      $types = node_type_get_types();
      node_add_body_field($types['product']);
      //New way to implement to add fields in your content type
      foreach (_test_installed_fields() as $field) {
          field_create_field($field);
      }
      foreach (_test_installed_instances() as $fieldinstance) {
          $fieldinstance['entity_type'] = 'node';
          $fieldinstance['bundle'] = 'product';
          field_create_instance($fieldinstance);
      }
      

      }

      /* *定义你的领域 */

      function _test_installed_fields() {
      $t = get_t();
      return array(
        'product_title123' => array(
          'field_name' => 'product_title123',
          'label' => $t('Product Title'),
          'type' => 'text'
        ),
        'description123' => array(
          'field_name' => 'description123',
          'label' => $t('Description'),
          'type' => 'text'
        ),
      
      );
      

      }

      /* * 定义你的字段实例 */

      function _test_installed_instances() {
      $t = get_t();
      return array(
        'product_title123' => array(
          'field_name' => 'product_title123',
          'type' => 'text',
          'label' => $t('Product Title'),
          'widget' => array(
            'type' => 'text_textfield'
          ),
          'display' => array(
            'example_node_list' => array(
              'label' => $t('Product Title'),
              'type' => 'text'
            )
          )
        ),
        'description123' => array(
          'field_name' => 'description123',
          'type' => 'text',
          'label' => $t('Description'),
          'widget' => array(
            'type' => 'text_textarea_with_summary'
          ),
          'display' => array(
            'example_node_list' => array(
              'label' => $t('Description'),
              'type' => 'text'
            )
          )
        ),
      
      );
      

      }

      /** * 实现 hook_uninstall()。 */

      function test_uninstall() {
      $ournewtype = 'product';
      $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
      $result = db_query($sql, array(':type' => $ournewtype));
      $nodeids = array();
      foreach ($result as $row) {
          $nodeids[] = $row->nid;
      }
      node_delete_multiple($nodeids);
      node_type_delete($ournewtype);
      

      }

      就是这样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-27
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多