0

I created new field field_csob_srcode in my custom code:

<?php
$field = array(
'field_name' => 'field_csob_srcode',
'type' => 'text',
'settings' => array(
  'max_length' => 4,
),
  );
 field_create_field($field);
?>

I would need to create custom formatter for this field. I am using such implementation of hook_field_formatter_info

<?php
function mymodule_field_formatter_info() {
  $info = array(
'csob_webpay_description' => array(
  'label' => t('SRCODE - text description'),
  'field types' => array('text'),
  'description' => t('Displays text description of message from CSOB GP     WebPay'),
),
  );
return $info;
}

It works ok, but now, I see my new formatter as option at all fields settings, which have type = 'text'. Is it possible to restrict using of formatter, not only by field type, but also by field? When I try to set field in hook_field_formatter_info:

'field types' => array('field_csob_srcode'),

then it does not work, formatter disappears from options in field display settings.

EDIT: I have not problem to set formatter for my field, I just need to restrict formatter ONLY for my field. So other text fields cannot use my formatter. So the formatter will not appear as option in selectbox, when selecting format for display

1
  • If memory serves correctly there's no such option, which means you're left with altering the manage fields form manually. Shouldn't be too tricky to do though
    – Clive
    Jun 13, 2015 at 12:36

1 Answer 1

0

Use default formatter in your field implemention

$field = array(
  'field_name' => 'field_csob_srcode',
  'type' => 'text',
  'settings' => array(
  'max_length' => 4,
  ),
  'default_formater' => 'csob_webpay_description',
);

it should works.

4
  • Thanks, but this does not work for me. I have not problem to set formatter for my field, I just need to restrict formatter ONLY for my field. So other text fields cannot use my formatter. So the formatter will not appear as option in selectbox, when selecting format for display. Jun 13, 2015 at 11:29
  • Have you implemented hook_field_info() ? Jun 13, 2015 at 11:56
  • No, but as I understand, I can create field also with method field_create_field($field), then I do't need hook_field_info. Or am I wrong? Jun 14, 2015 at 13:17
  • If you want to relate your widget only to your own custom field you should introduce your field to drupal as a field type , this is done using hook_field_info . Jun 15, 2015 at 12:28

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .