Drupal 6.x modul fejlesztése - Új CCK típus

Drupal 6-ban új CCK típust szeretnénk létrehozni.

Angolul itt olvashatunk erről: Creating a Compound Field Module for CCK in Drupal 6.x - by Jennifer Hodgdon
A mellékelt teljes példát lásd a csatolmányban.

Hogyan lehet CCK mezőt hook_update_N() függvényben létrehozni:  Automating CCK field creation with update hook/deployment

Hogyan lehet adminisztrációs felületről betölteni a korábban elmentett CCK-mezőket:
Define content types and CCK fields for your custom module using content_copy

Saját megoldás ez utóbbi útmutató alapján (Drupal 6.16, CCK 6.x 2.6):

Az alábbi példában egy "company" típus CCK kiegészítéséről van szó.
Valami miatt nekem az installból vagy a hook_update_N() függvényből tölténő létrehozás nem futott le, viszont a másik megoldás teljesen jól működik.
Azaz a kiexportált CCK mezőket az új modul (mymodule) könyvtárában lévő type mappába mentettem le type_<mezőnév>.txt fájlokba. A modul installálása után az adminisztrációs oldalán megjelenő hibaüzenet (amely az állapotjelentésben is megjelenik) tartalmazza azt a linket, amelyre kattintva lefut a CCK mezőket beolvasó függvény: mymodule_install_cck()

<?php
// $Id: mymodule.module
/**
* @file
* My Module
*/

/** -----------------------------------------------------------------------
* Node Info
**/
function mymodule_node_info() {
  return array(
    'my_company' => array(
      'name' => t('Company'),
      'module' => 'mymodule_company',
      'description' => 'Create a Company.',
      'title_label' => 'Name',
      'has_body' => FALSE,
    ),
  );
} // function mymodule_node_info
/** -----------------------------------------------------------------------
* Valid permissions for this module
* @return array An array of valid permissions for the mymodule module
*/
function mymodule_perm() {
  return array(
    t('view company'), t('create company'), t('administer company'),
  );
} // function mymodule_perm()
/** -----------------------------------------------------------------------
* Access Company
**/
function mymodule_company_access($op, $node, $account) {
  if ($op == 'view')   return user_access('view company',       $account);
  if ($op == 'create') return user_access('create company',     $account);
  if ($op == 'update') return user_access('administer company', $account);
  if ($op == 'delete') return user_access('administer company', $account);
} // function mymodule_company_access
/** -----------------------------------------------------------------------
* Form Company
**/
function mymodule_company_form(&$node) {
  $type = node_get_types('type', $node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => isset($node->title) ? $node->title : '',
    '#description' => 'Short name of the Company.',
    '#weight' => -5
  );
  return $form;
}
/** -----------------------------------------------------------------------
* Help
**/
function mymodule_help($path, $arg) {
  switch ($path) {
    case 'admin/help#mymodule':
      return '<p>' . t('This module was created by <Author Name>.') . '</p>';
      break;
  }
}
/** -----------------------------------------------------------------------
* Admin settings
*/
function mymodule_admin() {
  $form = array();
  $form['mymodule_ws_url'] = array(
    '#type' => 'textfield',
    '#title' => t('AAA-Something'),
    '#default_value' => variable_get('mymodule_something', ''),
    '#maxlength' => 225,
    '#description' => t("Put something here..."),
    '#required' => TRUE,
  );
  if( ! variable_get( 'mymodule_content_types_installed', 0 ) ){
    drupal_set_message(t('Click '.l('here','mymodule/install').' to install the content types and/or CCK fields for the %type module.', array('%type'=> $type)), "error");
  };
  return system_settings_form($form);
} // function mymodule_admin()
/** -----------------------------------------------------------------------
* Implementation of hook_menu
**/
function mymodule_menu() {
  $items = array();
  $items['admin/settings/mymodule'] = array(
    'title' => 'My Module',
    'description' => 'My Modul description...',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin'),
    'access arguments' => array('administer company'),
    'type' => MENU_NORMAL_ITEM,
   );
  // A Page for the modul
  $items['mymodule_general'] = array(  // The URL
    'title' => 'CustImp Checkbox',  // Title of the called page
    'page callback' => 'mymodule_general',  // The called function name provides the page content
    'access arguments' => array('view ci checkbox general'), // Who can see this menu
    'type' => MENU_NORMAL_ITEM,
  );
$items['mymodule/install'] = array(
    'page callback' => '_mymodule_install_cck',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
} // function mymodule_menu()
/** -----------------------------------------------------------------------
* Check that content types and fields have been installed
* if not, set a message in admin
*/
function mymodule_requirements($phase){
  $requirements = array();
  // Ensure translations don't break at install time.
  $t = get_t();
  $type = 'My Module';
  if ($phase == 'runtime') {
    //check if any cck fields exist for this type
    if( ! variable_get( 'mymodule_content_types_installed', 0 ) ){
      $requirements['mymodule_cck_warning'] = array(
        'title' => $t('My Module Content Types'),
        'value' => 'CCK fields not found',
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('Click '.l('here','mymodule/install').' to install the content types and/or CCK fields for the %type module.', array('%type'=> $type)),
      );
    }
  }
  return $requirements;
} // function project_requirements
/** -----------------------------------------------------------------------
* For using Require to load the CCK fields from the files in 
**/
function _mymodule_install_cck() {
  $subdir = "type";
  $prefix = "type_";
  $ext    = ".txt";
  if( ! user_access( 'administer content types' )){
    drupal_set_message( 'Sorry, You don\'t have permission to do that' );
    return drupal_access_denied();
  }
  if( ! variable_get( 'mymodule_content_types_installed', 0 ) ){
    drupal_set_message('Installing content types and cck fields');
    // Check if necessary module exists
    if (!module_exists('content_copy')) {
      module_enable(array('content_copy'));
      if (!module_exists('content_copy')) {
        $ret[] = array('succ module settings.ess'=>false, 'query'=>'Unable to enable content_copy module, can\'t continue.');
        return $ret;
      };
    };
    // Check if necessary function exists
    if (!function_exists('content_copy_import_form')) {
      $ret[] = array('success'=>false, 'query'=>'Unable to load necessary include files.');
      return $ret;
    };
    // Load the include files
    $dir = drupal_get_path('module', 'mymodule') ."/$subdir";
    $files = file_scan_directory($dir, '^'.$prefix.'.*'.$ext.'$', array('.', '..'), 0, FALSE, array('filename', 'name'));
    foreach ($files as $file) {
      $typename = substr($file->name, strlen($prefix));
      $export_data = file_get_contents($file->filename);
      $form_state = array();
      $form_state['values']['type_name'] = $typename;
      $form_state['values']['macro'] = $export_data;
      drupal_execute('content_copy_import_form', $form_state);
      $ret[] = array("success" => TRUE, "query" => "Loaded file: ".$file->filename);
    };
    variable_set( 'mymodule_content_types_installed', 1 );
  } else {
    drupal_set_message( 'Project content types are already installed.');
  };
  drupal_goto( 'admin/settings/mymodule' );
  return $ret;
} // function _mymodule_install_cck
/** -----------------------------------------------------------------------
* My Module General Page - page function
**/
function mymodule_general() {
  $content = "<h3>This is the My Module General Page.</h3>\n";
  return $content;
}
CsatolmányMéret
img_cap_tax_fld.zip13.65 KB