【问题标题】:Drupal 7 programatically create content type with existing fieldsDrupal 7 以编程方式使用现有字段创建内容类型
【发布时间】:2012-12-23 04:18:06
【问题描述】:

我发现,使用新字段和正文字段创建内容类型的问题似乎已记录并显示在多个位置。如何使用我在另一个模块中创建的现有字段类型以编程方式将字段类型包含到新内容类型中?

【问题讨论】:

    标签: drupal content-type


    【解决方案1】:

    将创建新内容类型的代码我们应该添加到.install 文件中。

    让我们添加hook_install():

    <?php
    function your_module_name_install() {
      // use get_t() to get the name of our localization function for translation
      // during install, when t() is not available.
      $t = get_t();
    
      // Define the node type.
      $node_example = array(
        'type' => 'node_example',
        'name' => $t('Example Node'),
        'base' => 'node_content',
        'description' => $t('This is an example node type with a few fields.'),
        'body_label' => $t('Example Description')
      );
    
      // Complete the node type definition by setting any defaults not explicitly
      // declared above.
      // http://api.drupal.org/api/function/node_type_set_defaults/7
      $content_type = node_type_set_defaults($node_example);
      node_add_body_field($content_type);
    
      // Save the content type
      node_type_save($content_type);
    }
    

    【讨论】:

    • 感谢您提供详细的示例,但是,我不确定我是否理解。我正在尝试创建一个新的内容类型,其中仅包含现有内容类型中的一个或 2 个字段,而不是现有内容类型或节点中的所有字段。如果我有一个内容类型 NewCType1,其中一个字段是 NewField,并且我需要一个也需要 NewField 的内容类型 NewCType2,您的描述是否允许我在内容类型 NewCType2 中包含 NewField?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2015-12-27
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多