PHP code example of netwerkstatt / silverstripe-opengraph-extension

1. Go to this page and download the library: Download netwerkstatt/silverstripe-opengraph-extension 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/ */

    

netwerkstatt / silverstripe-opengraph-extension example snippets


use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Extension;
use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Schema;

class SiteConfigSchemaExtension extends Extension
{
    /**
     * @return array<int, BaseType>
     */
    public function provideSchemaGraphNodes(SiteTree $page): array
    {
        $website = Schema::webSite()
            ->name($this->getOwner()->Title)
            ->url($page->getAbsoluteBaseURL());

        return [$website];
    }
}

use Netwerkstatt\Site\Page\BlockPage;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Extension;

class BlockPageSchemaItemsExtension extends Extension
{
    /**
     * @param array<int, object> $items
     */
    public function updateSchemaGraphItems(array &$items, SiteTree $page): void
    {
        if (!$page instanceof BlockPage) {
            return;
        }

        foreach ($page->ElementalArea()->Elements() as $element) {
            $items[] = $element;
        }
    }
}

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\SiteConfig\SiteConfig;
use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Schema;

class RoomType extends DataObject
{
    /**
     * @return array<int, BaseType>
     */
    public function provideSchemaGraphNodes(SiteTree $page, SiteConfig $siteConfig): array
    {
        $node = Schema::hotelRoom()->name($this->Title);
        return [$node];
    }
}