I have content with Title field filled in, URL left blank, and formatter set to Title as Link.

Title is displayed linked to main site's URL. No theme overrides.

Any hints why that might be?

Comments

drupalok’s picture

same problem here. it always renders as link.

Doneeo’s picture

I've got the same problem. I think its a bug, it worked onces.

ezheidtmann’s picture

I'm also having this problem. Will report back when I have a solution.

ezheidtmann’s picture

Status: Active » Needs review
StatusFileSize
new1.14 KB

This patch fixed my use case, but I haven't tested the other use cases or the other formatters.

The problem was that url() was interpreting a blank URL combined with 'absolute' => TRUE to mean "give me a link to the front page". In this patch, we only call url() if the link is not blank, and use the magic string <front> to link to the front page.

jcfiala’s picture

Status: Needs review » Fixed

Hmm.

It's a good idea for a patch, but the thing is that <front> is being removed because sending <front> through check_plain() results in &lt;front&gt;, which I think we can all agree isn't going to work.

What I've done instead is set the code so it only uses check_plain if the url isn't front!

benjifisher’s picture

StatusFileSize
new1.38 KB

#1891340: Empty url gets replaced with path to home page was marked as a duplicate.

I think this is the commit that fixes the issue: ba31bb0.

I tested, and the committed patch works for me.

I have attached a patch file for reference.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

denix’s picture

Title: Title + Optional URL not displayed as plain text » Title + Optional URL empty not displayed as plain text
Version: 7.x-1.x-dev » 7.x-1.1
Status: Closed (fixed) » Active

In link 7.x-1.1 the error is still there unfortunately.

benjifisher’s picture

Status: Active » Postponed (maintainer needs more info)

@denix:

It seems to work on my site. Can you give steps to reproduce?

denix’s picture

StatusFileSize
new130.19 KB

Hi, thanks for the prompt reply. In the file attached you can see the situation with 7.x-1.1 version of the link module.
The related links block is a view that render the links added in the content type and is positioned with a context.
In the composition you see, on the left how the links are rendered and in the bottom block is how they are inserted:
- External links is empty but appears as a link to the main website;
- title attribute is never displayed

and something really weird is that the block on the left is the same block, but rendered in edit page. As you can see the & char is rendered correctly!

Best regards and thanks!

benjifisher’s picture

As I said, it is working on my site, so I suspect something else is going on.

1. I doubt it makes a difference, but what do you get when you look at the node view page (node/1234 for example)? This would take Views and Context out of the picture.

2. What is the markup that generates your screen shot? Depending on your CSS, the following two snippets might display the same:
<li><a href="http://www.google.com">External links</a></li>
<li>External links</li>

3. What format/rewrite options are you using in Views, or what display options did you choose when configuring the field?

denix’s picture

Hi benjifisher,
thanks again. I have fixed the issue, rendering all the links in a blocks. This solve all the "title" related problems:
- External links is now showing correctly (an improvement maybe to add a span to the label i.e. <span>YourText</span>, so it is easier to customize via css);
- Title attribute is shown correctly.

But now a problem arise with the links themself:
link:
http://portal.unesco.org/en/ev.php-URL_ID=13084&URL_DO=DO_TOPIC&URL_SECT...
become:

<li class="views-row views-row-3 views-row-odd">
  <div class="row-wrapper">
    <div class="views-field views-field-field-link">
      <div class="field-content">
        <a target="_blank" href="http://portal.unesco.org/en/ev.php-URL_ID=13084&amp;amp;amp;URL_DO=DO_TOPIC&amp;amp;amp;URL_SECTION=201.html">Standards and Norms</a>
      </div>
    </div>
  </div>
</li>

Note the number of &amp; in the url!
:(

benjifisher’s picture

Status: Postponed (maintainer needs more info) » Fixed

That's a funny-looking URL. I bet the part after "ev.php" is converted into a query string on the UNESCO site.

It looks as though your URL is being run through htmlspecialchars() multiple times. (htmlspecialchars() is called from check_plain().) This function replaces & with &amp;. Applying it to &amp; give &amp;amp;. That is a separate issue, so I am going to mark this one fixed again. Please report it in a separate issue (after checking to see if it has already been reported).

I agree that an extra <span> would make formatting easier. Open up a feature request! If you are willing to write a custom module, you can also add a custom formatter, available from both views and the "manage display" tab on the node-edit page:

/**
 * Define custom formats for links.
 *
 * This involves hook_field_formatter_info(), hook_field_formatter_view(), and
 * hook_theme(). See the corresponding implementations in the Link module.
 */

/**
 * Implements hook_field_formatter_info().
 */
function mymodule_content_types_field_formatter_info() {
  $formatters = array();

  // Invoke this format when displaying lists of links. They will be displayed
  // with the title attribute as an annotation.
  $formatters['mymodule_content_types_link_annotate'] = array(
    'label' => t('Annotated link'),
    'description' => t('Display the title attribute as a plain-text annotation.'),
    'field types' => array('link_field'),
    'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  );

  return $formatters;
}

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

  switch ($display['type']) {
    case 'mymodule_content_types_link_annotate':
      foreach ($items as $item) {
        $elements[] = array(
          '#markup' => theme('link_formatter_' . $display['type'],
          array('element' => $item, 'field' => $instance, 'display' => $display)),
        );
      }
      break;

  return $elements;
}

/**
 * Implements hook_theme().
 */
function mymodule_content_types_theme() {
  return array(
    'link_formatter_mymodule_content_types_link_annotate' => array(
      'variables' => array('element' => NULL),
    ),
  );
}

/**
 * Hack the link formatter.
 *
 * We want a way to annotate links, and we want to add sub-headers in the list.
 * This hack pulls out title attributes and uses them as annotations, and it
 * adds some markup to non-links so that they can be styled as sub-headers.
 *
 * @see theme_link_formatter_link_default()
 */
function theme_link_formatter_mymodule_content_types_link_annotate($vars) {
  $element = $vars['element'];
  if (!isset($element['url']) || !$element['url']) {
    $text = check_plain(trim($element['title']));
    return '<span class="link-nourl">' . $text . '</span>';
  }
  else {
    if (isset($element['attributes']['title'])) {
      $note = check_plain($element['attributes']['title']);
      unset($element['attributes']['title']);
    }
    else {
      $note = '';
    }
    return theme_link_formatter_link_default($vars) . $note;
  }
}
denix’s picture

Amazing! thank you very much! I will now go to open the other two tickets!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.