Drupal Field API - 设置列表

时间:2015-05-11 14:00:53

标签: php drupal drupal-7

我正在尝试通过模块在Drupal中创建一个'link_field',我有以下代码但是我想配置字段设置来执行以下操作:

  • 不需要标题
  • 将值数更新为无限

我很难找到关于您可以为这些设置关键/配对值的设置的任何信息,任何能够提供这些指导的人都可以提供指导吗?

    <?php
/**
 * Implements hook_enable().
 *
 * Create a field. Fields can be created without any needs to attach them to
 * entities.
 */
function youtube_carousel_enable() {
  $field = array(
    'field_name' => 'ytcarousel_field',
    'type' => 'link_field',
  );
  field_create_field($field);


  /**
  * Bind field to a entity bundle.
  */
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'node',
    'bundle' => 'homepage',
    'label' => 'YouTube Video'
  );
  field_create_instance($instance);
}

/**
 * Implements hook_disable().
 *
 * Remove field from node bundle (content type) and then delete the field.
 */
function youtube_carousel_disable() {
  $instance = array(
    'field_name' => 'ytcarousel_field',
    'entity_type' => 'node',
    'bundle' => 'homepage',
    'label' => 'YouTube Video'
  );
  field_delete_instance($instance);
  field_delete_field($instance['field_name']);
  print 'Removed ' . $instance['field_name'] . "\n";
}
?>

干杯

1 个答案:

答案 0 :(得分:0)

在查看HTML源代码以获取键/值对后,我设法使用以下代码执行与上述类似的操作:

<?php
/**
 * Implements hook_enable().
 *
 * Create a field. Fields can be created without any needs to attach them to
 * entities.
 */
function youtube_carousel_enable() {
  $field = array(
    'field_name' => 'ytcarousel_field',
    'type' => 'link_field',
    'cardinality' => -1
  );
  field_create_field($field);


  /**
  * Bind field to a entity bundle.
  */
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'node',
    'bundle' => 'homepage',
    'label' => 'YouTube Video',
    'settings' => array('title' => 'required')
  );
  field_create_instance($instance);
}

/**
 * Implements hook_disable().
 *
 * Remove field from node bundle (content type) and then delete the field.
 */
function youtube_carousel_disable() {
  $instance = array(
    'field_name' => 'ytcarousel_field',
    'entity_type' => 'node',
    'bundle' => 'homepage',
    'label' => 'YouTube Video',
    'settings' => array('title' => 'required')
  );
  field_delete_instance($instance);
  field_delete_field($instance['field_name']);
  print 'Removed ' . $instance['field_name'] . "\n";
}
?>

基数控件有许多值可以拥有的实例,&#34; -1&#34;是无限的。

'cardinality' => -1

标题属性是通过实例控制的,而不是字段本身,我相信,它使用基于文本的值而不是数字。

'settings' => array('title' => 'required')