Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:56:30+00:00 2026-05-31T09:56:30+00:00

I started learning drupal customization, and i’m trying to create a very simply custom

  • 0

I started learning drupal customization, and i’m trying to create a very simply custom field for drupal.

I tried to follow several tutorial, but when i install the field (apparently without problem) it doesn’t appear into the field list.
But if i try to look at the source code, my field is with “hidden” attribute.

Actually i developed 2 files, the info file, and the module_file.

Here the code for module:

<?php
/**
 * @pricefield.module
 * add a price field.
 *
 */
 /**
 * Implements hook_field_formatter_info().
 */
function pricefield_field_formatter_info() {
  return array(
    'pricefield_custom_type' => array( //Machine name of the formatter
      'label' => t('Price'),
      'field types' => array('text'), //This will only be available to text fields
      'settings'  => array( //Array of the settings we'll create
        'currency' => '$', //give a default value for when the form is first loaded        
      ),            
    ),
  );
}
/**
 * Implements hook_field_formatter_settings_form().
 */
function pricefield_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];
  //This gets the actual settings
  $settings = $display['settings'];
  //Initialize the element variable
  $element = array();
  //Add your select box
  $element['currency'] = array(
    '#type'           => 'textfield',                           // Use a select box widget
    '#title'          => 'Select Currency',                   // Widget label
    '#description'    => t('Select currency used by the field'), // Helper text
    '#default_value'  => $settings['currency'],              // Get the value if it's already been set    
  );
  return $element;
}
/**
 * Implements hook_field_formatter_settings_summary().
 */
function pricefield_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('The default currency is: @currency ', array(
    '@currency'     => $settings['currency'],    
  )); // we use t() for translation and placeholders to guard against attacks
  return $summary;
}
/**
 * Implements hook_field_formatter_view().
 */
function pricefield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array(); // Initialize the var
  $settings = $display['settings']; // get the settings
  $currency = $settings['currency']; // Get the currency  
  foreach ($items as $delta => $item) {
    $price = $item['safe_value']; // Getting the actual value
  }  
  if($price==0){
      $element[0] = array('#markup' => 'Free');
  } else {
      $element[0] = array('#markup' => $currency.' '.$price);
  }
  return $element;
}
?>

I’m not sure if the problem is a missing installation file. I tried to look at several of them but they are so different.
I don’t understand how to add my custom field to the database (i think it is needed). I must do a query? Or i must use some function.

I need to make a mymodule_install method? Or in that case is needed only the mymodule_field_schema? (looking at different basic module, some of them implement only that function, but other implement an insatll method, and not the field_schema).

So for example if i want to add my custom field that will be a string, and it need only a textbox what i still need to do, in order to have my field available on drupal?

Basically i don’t need a new widget for my custom field, i want to use the usual Text Widget already available in drupal.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:56:31+00:00Added an answer on May 31, 2026 at 9:56 am

    If I understand you right you need to implement hook_field_widget_info_alter() and tell Drupal that the textfield widget can be used by your field:

    function pricefield_field_widget_info_alter(&$info) {
      // 'pricefield' will be whatever the machine name of your field is
      $info['text_textfield']['field types'][] = 'pricefield';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started learning Emacs . I went through the tutorial, read some introductory
I have just started learning Erlang and am trying out some Project Euler problems
I started learning jQuery just yesterday, and I like it very much. And now
Just started learning Rails (3). I am tearing my hair out trying to find
I am learning drupal and am trying to add some extra features to a
I started learning C# a week ago. Today I was trying to install the
I just started learning blueprint css , with the tutorial a closer look at
Just started learning NServiceBus and trying to understand the concept. When it talks about
I started learning mysql c++ connector. I was trying with sample code given over
I started learning Django recently and am having a strange problem with the tutorial.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.