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?).
'field_types'
value in your hook_field_widget_info. It should bearray('add_referenced_content')
right?