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

  • 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 '15 at 12:36
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.

| improve this answer | |
  • 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. – tomas.teicher Jun 13 '15 at 11:29
  • Have you implemented hook_field_info() ? – Alireza Tabatabaeian Jun 13 '15 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? – tomas.teicher Jun 14 '15 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 . – Alireza Tabatabaeian Jun 15 '15 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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