Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable anchors to (other) internal pages #11

Open
BrandSven opened this issue Sep 27, 2022 · 1 comment
Open

Enable anchors to (other) internal pages #11

BrandSven opened this issue Sep 27, 2022 · 1 comment

Comments

@BrandSven
Copy link

BrandSven commented Sep 27, 2022

Hi, thanks for this very useful plugin. :-)

Here's an idea for a new feature:
Right now, one can either link to another internal page, OR to an anchor on the current page.

Many times we need to do both - link to an anchor on ANOTHER internal page. This could backend-wise easily be implemented by showing the link-internal field next to the link-anchor field:

src/Forms/LinkItemField.php, Line 157+:

TreeDropdownField::create('InternalLinkID', 'Internal Link', SiteTree::class)
  ->addExtraClass('link-hidden link-internal link-anchor'),
                                             ^^^^^^^^^^^

The final link could be something like:
/about-us/#map

src/Model/LinkItem.php, Line 178-180:

switch($this->LinkType) {
  case 'anchor':
      $link = ($this->InternalLink()->Link() ? : '') . '#'.$this->Anchor;

If you like, I can create a merge request. Not sure though if LinkItem->getCMSFields() needs to be adjusted, too. I'm fairly new to Silverstripe.

@davejtoews
Copy link
Contributor

We've solved this same thing by adding this in a DataExtension.

    public function updateCMSFields(FieldList $fields)
    {
        $fields->replaceField(
            'Anchor',
            TextField::create('Anchor', 'Anchor Link (without #)')
                ->displayIf('LinkType')->isEqualTo('anchor')->orIf('LinkType')->isEqualTo('internal')->end(),
        );
    }

    public function updateLink(&$link)
    {
        if ($this->owner->LinkType == 'internal') {
            // Use absolute URL in case the reference is on another subsite
            $link = $this->owner->InternalLink()->AbsoluteLink();
            // Add Anchor tag if added in custom field
            $link .= $this->owner->Anchor ? '#' . $this->owner->Anchor : '';
        }
    }

Would be great to have this integrated in the module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants