2

I want to open same page from different contents. So I need to generate link like /testimonials#group-1 for these contents. I used pathauto module for this. I made pattern for this category testimonials#[node:title]. But this URL get encoded by browser /testimonials%23group-1 and it gives me no page found error.

So how can I put anchor (#) in URL alias which can give me output like /testimonials#group-1.?

Thanks in advance...

  • Why would you be using pathauto for this? It is not actually part of the "path", since it is only an anchor. We will be needing more info about where you are trying to implement this. – Ambidex Feb 18 '13 at 10:44
  • @NeverQuit Please read Ambidex's comment. Fragments are not part of the path, it doesn't make sense to add a fragment to a URL alias (and you can't do it). You should add the fragment to the end of a URL in a link that points to the path – Clive Feb 18 '13 at 11:10
  • @Ambidex I'm using this at one of my content->URL path settings-> URL alias = "testimonials#group-1". But browser encode '#' to %23 so it gives an error. – Never Quit Feb 18 '13 at 11:36
  • 1
    @MPD From a "this is what I'd like" point of view it does make sense, but from a practical standpoint it's not implementable. Ambidex's answer goes a good way towards explaining why that is – Clive Feb 18 '13 at 12:03
  • 1
    @MPD That would be a piece of "logics" implementation for your summary of nodes, it still is not a reference to your node itself but a position in a summary of nodes, so that should not be in a path (nor alias). – Ambidex Feb 18 '13 at 12:15
3

I think you are trying to add anchors to a node's alias to define multiple sections in a node, which is impossible since an anchor destination is not part of a path and therefor not part of an alias. For more information, see the W3 introduction on links in HTML.

Creating a anchor: If you would like to subsection your node's body, you should use anchors in your code:

<a name="anchorOne"> ... </a>

If you're using the WYSIWYG module, make sure you've got the Anchor plugin enabled, so you can create the anchors through the nicely builtin anchor plugin's GUI, which makes it that much easier.

Linking to an anchor: After creating an anchor, you can link to that section by making a hyperlink as follows:

<a href="/path/to/node#anchorOne">Link text</a>

To build the above link in the Drupal-way:

<?php print l('Link text', 'path/to/node', array('fragment' => 'anchorOne')); ?>

Or to only build the URL including the anchor destination:

<?php print url('path/to/node', NULL, 'anchorOne'); ?>
1

It's called a fragment identifier, and is done with the l() function:

$options = array('fragment' => 'group-1');
l('Anchor', 'path', $options);
0

First of all, set the Pathauto pattens for the content type testimonial to /testimonials/[node:title]. This could later on be useful if you display one testimonial as a node, for instance if you don't include the full node in the view below.

Then create a view that lists all your testimonials, and show them as a page on the URL /testimonials. Use the style Unformatted, and make sure you include the Title in the rows class (using the "Style > Style > Settings" in views), you could be use Anchor-[title] (show you later on why the 'Anchor_'-part).

Then make a special versions of the views-view-unformatted.tpl.php (eg. views-view-unformatted--.tpl.php) and put it in your own themes folder (where it fits).

<?php foreach ($rows as $id => $row): ?>
  <div <?php if ($classes_array[$id]) { print 'class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
<?php endforeach; ?>

Make a small PHP snippet that fetches the rows styles and then find the one class that contains the "Anchor-Title".

<?php foreach ($rows as $id => $row): ?>
  <div <?php if ($classes_array[$id]) { print 'class="' . $classes_array[$id] .'"';  } ?>>
    <?php
      if ($classes_array[$id]) {
        $classes = explode(" ", $classes_array[$id]);
        for ($i=0;$i<count();$i++) {
          if (strpos($classes[$i], "Anchor_") === 0) {
            list($tmp, $anchor) = explode("_", $classes[$i]);
    ?>
    <a name="<?php print $anchor; ?>"></a>
    <?php
          }   
        }
      }
    ?>
    <?php print $row; ?>
  </div>
<?php endforeach; ?>

Perhaps the code can be optimized, but that's only something to get you started...

BTW: To create a "menu" on the top, you could add a header to the view that's containing a view that makes links out of titles. Eg.

  <a href="#Title">Link text</a>

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.