PHP code example of arillo / silverstripe-links

1. Go to this page and download the library: Download arillo/silverstripe-links library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arillo / silverstripe-links example snippets


use SilverStripe\ORM\DataObject;
use Arillo\Links\Link;
use Arillo\Links\LinkExtension;

class MyDataObject extends DataObject
{
    public function getCMSFields()
      {
          $this->beforeUpdateCMSFields(function($fields) {
              // in case you use Link::EDITMODE_PLAIN, you might need
              // to remove the link relation field generated by the scaffolder.
              $fields->removeByName(LinkExtension::FIELD . 'ID')
              $fields->addFieldsToTab(
                  'Root.Main',
                  // add link fields directly to the belonging DataObject.
                  Link::edit_fields(
                      $this,
                      [
                          'mode' => Link::EDITMODE_PLAIN,
                          'showLinkTitle' => true,
                      ]
                  )

                  // or use default editing via HasOneButtonField
                  Link::edit_fields($this)
              );
          });
          return parent::getCMSFields();
      }
}


namespace Arillo\Extensions;

use SilverStripe\Forms\DropdownField;
use Arillo\Elements\ElementBase;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
use Page;

class LinkExtension extends DataExtension
{
  private static $has_one = [
    'AnchorElement' => ElementBase::class,
  ];
  // alter cms fields
  public function updateLinkCMSFields(
    FieldList $fields,
    DataObject $holderRecord,
    array $config = []
  ) {
    $fieldsPrefix = $config['fieldsPrefix'];
    if ($this->owner->PageID) {
      $fields->push(
        DropdownField::create(
          "{$fieldsPrefix}AnchorElementID",
          'Anker-Element',
          $this->owner
            ->Page()
            ->Elements()
            ->map()
            ->toArray()
        )
          ->setEmptyString('[keins]')
          ->displayIf("{$fieldsPrefix}Type")
          ->isEqualTo('internal')
          ->end()
      );
    }
  }

  // alter href
  public function updateLinkHref($href)
  {
    if (
      $href &&
      $this->owner->Type == 'internal' &&
      $this->owner->AnchorElement()->exists()
    ) {
      $href .= "#{$this->owner->AnchorElement()->URLSegment}";
    }

    return $href;
  }
}