Skip to content

Commit

Permalink
Allow updating of URL in oEmbed block layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Apr 8, 2024
1 parent 2c1e292 commit fb88c63
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions application/src/Site/BlockLayout/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Oembed extends AbstractBlockLayout implements TemplateableBlockLayoutInter

protected $defaultData = [
'url' => null,
'url_old' => null,
'oembed' => null,
'refresh' => false,
];
Expand All @@ -39,7 +40,9 @@ public function onHydrate(SitePageBlock $block, ErrorStore $errorStore)
if (is_string($data['oembed'])) {
$data['oembed'] = json_decode($data['oembed'], true);
}
if (!$data['oembed'] || $data['refresh']) {
// Get the oEmbed response when a) the block is new, b) the user wants
// to referesh the existing oEmbed, c) the user changes the oEmbed URL.
if (!$data['oembed'] || $data['refresh'] || $data['url'] !== $data['url_old']) {
$data['oembed'] = $this->oembed->getOembed($data['url'], $errorStore);
}
$block->setData($data);
Expand All @@ -49,29 +52,30 @@ public function form(PhpRenderer $view, SiteRepresentation $site, SitePageRepres
{
$data = $block ? $block->data() + $this->defaultData : $this->defaultData;
$form = new Form\Form;

$form->add([
'type' => Form\Element\Url::class,
'name' => 'o:block[__blockIndex__][o:data][url]',
'options' => [
'label' => 'oEmbed URL', // @translate
],
'attributes' => [
'value' => $data['url'],
'required' => true,
],
]);
if (!$data['oembed']) {
$form->add([
'type' => Form\Element\Url::class,
'name' => 'o:block[__blockIndex__][o:data][url]',
'options' => [
'label' => 'oEmbed URL', // @translate
],
'attributes' => [
'value' => $data['url'],
'required' => true,
],
]);
return $view->formCollection($form, false);
}
$form->add([
'type' => Form\Element\Text::class,
'name' => 'oembed_url',
'type' => Form\Element\Url::class,
'name' => 'o:block[__blockIndex__][o:data][url]',
'options' => [
'label' => 'oEmbed URL',
'label' => 'oEmbed URL', // @translate
],
'attributes' => [
'value' => $data['url'],
'disabled' => true,
'required' => true,
],
]);
$form->add([
Expand All @@ -95,7 +99,7 @@ public function form(PhpRenderer $view, SiteRepresentation $site, SitePageRepres
]);
$form->add([
'type' => Form\Element\Hidden::class,
'name' => 'o:block[__blockIndex__][o:data][url]',
'name' => 'o:block[__blockIndex__][o:data][url_old]',
'attributes' => [
'value' => $data['url'],
],
Expand All @@ -109,12 +113,12 @@ public function form(PhpRenderer $view, SiteRepresentation $site, SitePageRepres
]);
return sprintf(
'%s<a href="#" class="expand" aria-label="expand"><h4>%s</h4></a><div class="collapsible">%s%s%s%s</div>',
$view->formRow($form->get('oembed_url')),
$view->formRow($form->get('o:block[__blockIndex__][o:data][url]')),
$view->translate('Advanced'),
$view->formRow($form->get('oembed_oembed')),
$view->formRow($form->get('o:block[__blockIndex__][o:data][refresh]')),
$view->formRow($form->get('o:block[__blockIndex__][o:data][url]')),
$view->formRow($form->get('o:block[__blockIndex__][o:data][oembed]')),
$view->formRow($form->get('o:block[__blockIndex__][o:data][url_old]')),
);
}

Expand Down

0 comments on commit fb88c63

Please sign in to comment.