Content Copy module allows you to paste in the definition of a CCK type, complete with fields. The attached patch makes it possible for modules to implement hook_default_fields(), and return a keyed array containing just the 'fields' portion of that definition array. When a module is enabled, it can use that hook to automatically ad CCK fields to any node types it defines using hook_node_info().

Code's still rough, and could definitely use some brainstorming, but I think it's the early stage of something very necessary for 6.0 inclusion.

Comments

eaton’s picture

StatusFileSize
new8.39 KB

Uh, this time? I include the patch. :-)

yched’s picture

It might be a little early for targetting hook_default_fields for the drupal 6 'cck in core', as it will possibly differ a lot from current CCK (see the FAPi3 discussion on devel ML, and the wiki pages Karen set up from that at http://groups.drupal.org/node/2717), but even without seeing this far, the functionality would be very valuable for the present 5 cycle.

I quickly browsed through the patch without applying, and the refactoring looks nice.
The question of when do we trigger the hook seems more problematic.
Ideally, this would be when the declaring module gets enabled.
But all the required field types might not be available at this time, so the user should be able to enable the required field modules, and have the fields added as well.
So the right time might be on module page submission.

But I guess we also want the user to be able to alter these 'default field definitions' (just like he can alter default module-defined views). If he does, what happens the next time he submits his module page so that his modifications are not overwritten ?

Anyway thanks Jeff for another great patch :-)

karens’s picture

I haven't actually tried the patch, just looked through it. Conceptually this makes sense. One problem, as yched said, is when to trigger it and how to treat subsequent calls to the hook (overwrite? ignore?). There could be times when you want to overwrite to make sure that your new node type is *not* altered from the 'parent' type and other times when you only want it to happen one time and never again.

Do you have some particular use cases in mind? Maybe thinking through a specific use case would help clarify the best way to work this out.

moshe weitzman’s picture

subscribing ... would be nice ...

yched’s picture

http://www.drupal.org/node/142521 has been marked duplicate

mikey_p’s picture

Subscribing

I really like the way this is laid out, however, I would still like to see a see an import API that could be used from, you guessed it, an Install Profile.

I have a patch thats moving in that direction, but I can't make it work nice without losing existing functionality without duplicating some code from content_copy_import_form_submit(). It can probably be done but I lack the ninja skills to do it.

Any thoughts on hook_default_fields in addition to a content_copy_import API type function?

-Mikey P

mikey_p’s picture

Nevermind....I should really read the patches better.

Looks like content_copy_add_fields_to_type($type_name, $fields) is exactly what I want.

I'll give this some testing but it looks like a big +1 to me. Thanks Eaton

(I really need to spend more time in the issue queue)

eaton’s picture

I will say, for the record, that I think this approach has a number of issues that make it ... less than ideal as CCK's long-term solution. Better separation of the CRUD functions and the form handling logic is what's ultimately needed.

somebodysysop’s picture

Can someone explain how to use the new patch functionality?

Let's say I'm creating an installation script for a 5.x module that creates a new node type, and I want to add a simple text (checkboxes) field to the node type I just created. How do I do that?

I was looking for CCK API, that let me to an issue that asked the exact same question, and the answer was to look at this thread. I see the patch, and someone mentioned the function content_copy_add_fields_to_type($type_name, $fields), but I can't find any reference on how to actually use any of it.

Thanks for any assistance.

sirkitree’s picture

Yes this would be very nice functionality; being able to define cck fields within the hook_node_info() something like this:

/**
 * Implementation of hook_node_info().
 */
function smackdown_node_info() {
  return array(
    'smackdown' => array(
      'name' => t('Smackdown'),
      'module' => 'smackdown',
      'description' => t('A smackdown pairs two things together and lets users vote on their favorite.'),
      'fields' => array(
        0 => array(
          'widget_type' => 'nodereference_autocomplete',
          'label' => 'Contestant #1',
          'weight' => '-8',
          'description' => '',
          'default_value_widget' => array(
            'field_contestant_1' => array(
              0 => array(
                'node_name' => '',
              ),
            ),
          ),
          'default_value_php' => '',
          'required' => '1',
          'multiple' => '0',
          'referenceable_types' => array(),
          'advanced_view' => '--',
          'advanced_view_args' => '',
          'field_name' => 'field_contestant_1',
          'field_type' => 'nodereference',
          'module' => 'nodereference',
          'default_value' => array(
            0 => array(),
          ),
        ),        
      ),
    ),
  );
}
ben_scott’s picture

Hi - great patch, just what I was looking for. I am having one problem though - if I try to specify a default value for a field, I get the error 'The default value is invalid'. Been playing around with this for a while now but getting nowhere - does anyone know a way round this?

@SomebodySysop - as I understand it, you create an array of field definitions (which you can get from exporting a content type), and return this array from hook_default_fields().

magico’s picture

Crucial! (subscribing)

darren oh’s picture

Version: 6.x-1.x-dev » 5.x-1.x-dev
Assigned: Unassigned » darren oh

Since I need this for a Drupal 5 project I'm working on, and no one has gotten this committed yet, I'm modifying the issue and assigning it to myself.

darren oh’s picture

StatusFileSize
new7.36 KB

Starting by bringing the patch up to date.

karens’s picture

Before you spend a lot of time on this, I want to let you know that all of these things work completely differently in 6.x, so this solution will not be anything that can be ported to 6.x.

darren oh’s picture

No reason to wait for Drupal 6. The changes are small, and mostly need better documentation.

joachim’s picture

Subscribing. This would be a huge addition to CCK and open up tons of possibilities.

What do you mean by better documentation?
If you mean documentation for programmers to use this new feature, I can help. Give me rough notes & bullet points and I can work them up into handbook pages.

Bobuido’s picture

This sounds like a great idea - A CCK gallery profile - That would be nice

Until then looks like I have to work it out myself - Which could take some time...

Will be watching this effort closely

juanjo_vlc’s picture

HI,
I think this feature is a "must have" for content type developers.

For example, I'm developing a large data structure build over smaller ones, and I want to use node references to the small pieces, so it would be great than I can add on the form a node reference like I do with a textfield or button, a simply

$form['myelement'] = array(
  '#type' => 'node_referece',
  '#datasource' => 'view/custom-view');

or

$form['myelement'] = array(
  '#type' => 'node_referece',
  '#node types' => array('page','history'));

For the website I'm developing is not a problem, because I can login and add a field using "the mouse way", but it will be harder to distribute the module.

Thanks for your time, and congratulations for your work.

bomarmonk’s picture

Any update on this functionality for Drupal 6: I would like to have a certain field copy over content automatically from a previous node (of the same type) with the same field.

jlevy’s picture

I too would like to see this feature in Drupal 6. Could someone please provide a status update on this?

mitchell’s picture

I reference this issue here: #72509: Expose a "Save content type" action.

jlevy’s picture

Version: 5.x-1.x-dev » 6.x-2.x-dev
Category: task » feature
Status: Needs work » Active

I'm not hearing anything from Darren Oh, so I'm changing the values based on #15.

@Darren Oh - are you still working on this?

darren oh’s picture

Assigned: darren oh » Unassigned

No.