0

In short: I want to create a field 'Referenced Content' that links to /node/add/content-type/. My actual question is: Why is there no option ( see my hook_field_info() function) in my select list in Manage Fields?

Basically I'm just learning the beginnings of module development and I'm trying to make small steps. So the first thing I want working would be to add a field width a widget that would be part of the configuration options in in admin/structure/types/manage/content-type/fields

I looked at the field_example module that is part of the examples module and here's my code so far (why I think custom code is needed look below).

<?php 
function entityreference_add_new_field_info() {
  return array(
    // We name our field as the associative name of the array.
    'add_referenced_content' => array(
      'label' => t('Referenced Content'),
      'description' => t('A button that allows content author to add new content that is 
          referenced.'),
      'default_widget' => 'entityreference_add_new_button',
      'default_formatter' => 'entityreference_add_new_button',
    ),
  );
}

function entityreference_add_new_field_formatter_info() {
  return array(

    'entityreference_add_new_button' => array(
      'label' => t('Simple HTML link'),
      'field types' => array('entityreference_add_new_button'),
    ),
  );
}

function entityreference_add_new_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {

    case 'entityreference_add_new_button':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#type' => 'html_tag',
          '#tag' => 'a',
          '#attributes' => array(
            'href' => '#',
          ),
          '#value' => t('This button is currently linking to @code', array('@code' => '#')),
        );
      }
      break;
  }
   return $element;
}

function entity_reference_add_new_field_widget_info() {
  return array(
    'entityreference_add_new_button' => array(
      'label' => t('Button to make new referenced content'),
      'field types' => array('entityreference_add_new_button'),
    ),
  );
}

I left out the validate functions and the is_empty() functions thinking that they have no influence on showing up in the Manage Fields configuration page or not.

In case you're wondering why I'm thinking custom code instead of just adding a field with some markup here's the end goal:

An example is that there is a contenttype 'transfer order' that is referenced to contenttype 'transfer file'. There is a one to many relationship between a transfer file and transfer orders respectively. I have a view setup for the content type 'transfer file' that lists it's title, date and edit field. Added to this view, I will be needing a field that is a link to a node edit page. Like this node/add/transport-order/<nid>. needs to be the id of the transfer order content type. The idea then is that fields in the new transfer order would be already filled in with content that comes from a transfer file that is referenced.

note: I posted another question where I initially thought I needed to do something that the references dialog module would already do. So I guess now this doesn't seem to be the case and should call for some custom code.

Update: So I tried reducing my code to just the hook_field_info() to feed the field_create_field() function which should get me started getting the field into my Manage Fields page as a babystep.

code in my .module file would be like this:

function entityreference_add_new_field_info() {
  return array(
       'entityreference_add_link' => array(
          'label' => t('Entityreference link'),
          'description' => t('Entityreference link'),
          'default_widget' => 'entityreference_link_widget',
          'default_formatter' => 'entityreference_link_formatter',
      ),
    );  
  field_create_field('entityreference_add_link');
}

This doesn't put anything in my Manage Page table under 'Field' (Should be an option in my drop down list, right?).

1
  • I think you have wrong 'field_types' value in your hook_field_widget_info. It should be array('add_referenced_content') right?
    – Beebee
    Commented May 13, 2016 at 16:03

1 Answer 1

-1

The only thing required for your field to show up in your "Manage Fields" page is to create a field with field_create_field(), you may define the field using hook_field_info().

It can be cumbersome to those settings arrays from scratch. I would recommend this approach. Build your field using your field UI, and install Devel.

Then view this blog post: http://steindom.com/articles/exporting-and-creating-field-definitions-drupal-7

What you really care about in that blog post is: field_create_field($info_config);

So in short:

  • build your field using drupals FieldUI (Manage Fields -> Add field ....)
  • export the arrays to code via the above blog post
  • copy that code into your module install() or enable() function.
3
  • That looks like some stuff. Have to admit i'm already stuck at combining the hook_field_info() and 'field_create_field()'. 'hook_field_info()' returns an array that I can't seem to get "into" my 'field_create_field()' function. I did '$new_field = modulename_field_info()' but then it says call to undefined function field_create_field().
    – Immers
    Commented Jun 26, 2012 at 12:04
  • Maybe field_create_field() only workins in the .install file?
    – Immers
    Commented Jun 26, 2012 at 12:45
  • hook_field_info should be used if you're defining new field types. If you're using existing field types (text field, list, etc.), then I don't think this is what you want. If you want to create new fields, just get your field definitions set up as described above, and use field_create_field in your hook_enable or a hook_update_N (depending on where you are in development). Also, once your field is created, be sure to attach it to an entity using field_create_instance. Commented Feb 28, 2015 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.