0

I'm trying to change the default link generate by the email_field module so It shows an image instead of the text (Contact person by email).

I went into the module and foun the part I need to change, but because I'm new to drupal and php I have no clue how to change it...

The code I think I need to override:

function email_field_formatter_view($object_type, $object, $field, $instance,    $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) { 

    case 'email_contact':
      $ids = entity_extract_ids($object_type, $object);
      foreach ($items as $delta => $item) {
        $element[$delta] = array('#markup' => l(t('Contact person by email'), 'email/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']));
        // Since email is always sent to first item's email, break after any email address found.
        break;
      }
      break;
  }
}

The contact person by email is the part I want to change to
'< div class="email_image">< /div>'

Anyone who can help with this? Or knows where I can find some help?

Thx up front.

  • why not use css to replace the text with image? – Aboodred1 May 9 '13 at 17:26
  • Hey Aboodred1, that would be the perfect solution. But if I do that do I keep the function of the link? I know I should be able to do this with css, but I'm stuck on how to do it. – Jimmy Raets May 9 '13 at 18:22
1

You have two options to solve your issue:

CSS Option: You can replace text with image using css, add the following code to your css file and make sure to replace field name accordingly

.field-name-field-custom .field-item a {
  display: block;
  text-indent: -99999em;
  background-image: url('../images/image_name.ext');
  background-position: left top;
  background-repeat: no-repeat;
}

Note: field-custom should be replaced with your email field

Field Formate Option: You can create a new field formate in a new module and then you can choose from Field UI the new formate that you created.

Implement the following code in a Drupal module.

/**
 * Implements hook_field_formatter_info().
 */
function mymodule_field_formatter_info() {
  return array(
    'email_contact_image' => array(
      'label' => t('Email contact form image link'),
      'description' => t('Display an image link to a contact form.'),
      'field types' => array('email'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {

    case 'email_contact_image': 

      $ids = entity_extract_ids($entity_type, $entity);

      if ($ids[0] !== NULL) {
        foreach ($items as $delta => $item) {

          $img_vars = array(
            'path' => drupal_get_path('theme', 'THEME_NAME') . '/images/IMAGE_NAME.EXT', 
            'alt' => t('Contact person by email'),
            'title' => t('Contact person by email'),
            'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
          );
          $img = theme('image', $img_vars);

          $options['html'] = TRUE;

          $element[$delta]['#markup'] = l($img, 'email-contact/' . $entity_type . '/' . $ids[0] . '/' . $instance['field_name'], $options);
          // Since email is always sent to first item's email, break after any email address found.
          break;
        }
      }
      break;
  }

  return $element;
}

Notes:

  • make sure to replace mymodule with your own module name
  • make sure to replace THEME_NAME with your own theme name
  • make sure to replace IMAGE_NAME.EXT with your own image name

Creating modules - tutorial

  • Thanks alot, the first option is perfect. I knew it was possible but I just did something wrong with my css. Anyhow, Aboodred1 thx for the clear explanation you just made my day :) . – Jimmy Raets May 9 '13 at 20:14
  • Glad it worked for you :) – Aboodred1 May 9 '13 at 20:22

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.