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

Add support for opengraph handler #9

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Netgen\IbexaFieldTypeHtmlTextBundle\DependencyInjection;

use Netgen\Bundle\OpenGraphBundle\NetgenOpenGraphBundle;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -13,6 +14,7 @@
use Symfony\Component\Yaml\Yaml;

use function file_get_contents;
use function in_array;

class NetgenIbexaFieldTypeHtmlTextExtension extends Extension implements PrependExtensionInterface
{
Expand All @@ -27,6 +29,13 @@ public function load(array $configs, ContainerBuilder $container): void
);

$loader->load('services.yaml');

/** @var array<class-string> $activatedBundles */
$activatedBundles = $container->getParameter('kernel.bundles');

if (in_array(NetgenOpenGraphBundle::class, $activatedBundles, true)) {
$loader->load('opengraph.yaml');
}
}

public function prepend(ContainerBuilder $container): void
Expand Down
5 changes: 5 additions & 0 deletions bundle/Resources/config/opengraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
Netgen\IbexaFieldTypeHtmlText\OpenGraph\HtmlText:
parent: netgen_open_graph.handler.field_type.abstract
tags:
- { name: netgen_open_graph.meta_tag_handler, alias: field_type/nghtmltext }
2 changes: 1 addition & 1 deletion lib/FieldType/SearchFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getIndexData(Field $field, FieldDefinition $fieldDefinition)
new Search\Field(
'value',
$this->extractShortText($field->value->data),
new Search\FieldType\StringField(),
new StringField(),
),
new Search\Field(
'fulltext',
Expand Down
34 changes: 34 additions & 0 deletions lib/OpenGraph/HtmlText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Netgen\IbexaFieldTypeHtmlText\OpenGraph;

use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException;
use Netgen\Bundle\OpenGraphBundle\Handler\FieldType\Handler;
use Netgen\IbexaFieldTypeHtmlText\FieldType\Value;

use function str_replace;
use function strip_tags;
use function trim;

final class HtmlText extends Handler
{
/**
* @param array<mixed> $params
*/
protected function getFieldValue(Field $field, string $tagName, array $params = []): string
{
if (!$this->fieldHelper->isFieldEmpty($this->content, $field->fieldDefIdentifier)) {
return trim(str_replace("\n", ' ', strip_tags($field->value->text)));
}

throw new FieldEmptyException($field->fieldDefIdentifier);
}

protected function supports(Field $field): bool
{
return $field->value instanceof Value;
}
}
Loading