View source
<?php
namespace Drupal\entity_browser_generic_embed\Plugin\EntityBrowser\Widget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Form\FormStateInterface;
class EmbedCode extends EntityFormProxy {
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
$form['input'] = [
'#type' => 'textarea',
'#placeholder' => $this
->t('Enter a URL...'),
'#attributes' => [
'class' => [
'keyup-change',
],
],
'#ajax' => [
'event' => 'change',
'wrapper' => 'entity',
'method' => 'html',
'callback' => [
static::class,
'ajax',
],
],
"#description" => $this
->getEmbedDescription($form_state),
];
return $form;
}
public function validate(array &$form, FormStateInterface $form_state) {
$value = trim($this
->getInputValue($form_state));
if ($value) {
parent::validate($form, $form_state);
}
else {
$form_state
->setError($form['widget'], $this
->t('You must enter a URL or embed code.'));
}
}
public function getEmbedDescription(FormStateInterface $form_state) {
$embedDescription = "<h5>" . $this
->t("You can embed any of the following media by pasting a single complete URL:") . "</h5>";
$embedDescription .= "<ul>";
$mediaTypes = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
$mediaFileSources = [
'Drupal\\media\\Plugin\\media\\Source\\File',
'Drupal\\media\\Plugin\\media\\Source\\Image',
'Drupal\\media\\Plugin\\media\\Source\\AudioFile',
'Drupal\\media\\Plugin\\media\\Source\\VideoFile',
];
$allowedBundles = $this
->getAllowedBundles($form_state);
if (isset($allowedBundles) && is_array($allowedBundles) && count($allowedBundles) > 0) {
foreach ($mediaTypes as $mediaType) {
$mediaTypeSource = get_class($mediaType
->getSource());
if (isset($mediaTypeSource) && !in_array($mediaTypeSource, $mediaFileSources)) {
if (in_array($mediaType
->id(), $allowedBundles)) {
$embedDescription .= "<li>";
$embedDescription .= $mediaType
->label();
$source_configuration = $mediaType
->getPluginCollections();
if (isset($source_configuration['source_configuration'])) {
$configuration = $source_configuration['source_configuration']
->getConfiguration();
if (isset($configuration['providers']) && is_array($configuration['providers']) && count($configuration['providers']) > 0) {
$embedDescription .= " (" . implode(', ', $configuration['providers']) . ")";
}
}
$embedDescription .= "</li>";
}
}
}
}
else {
foreach ($mediaTypes as $mediaType) {
$mediaTypeSource = get_class($mediaType
->getSource());
if (isset($mediaTypeSource) && !in_array($mediaTypeSource, $mediaFileSources)) {
$embedDescription .= "<li>";
$embedDescription .= $mediaType
->label();
$source_configuration = $mediaType
->getPluginCollections();
if (isset($source_configuration['source_configuration'])) {
$configuration = $source_configuration['source_configuration']
->getConfiguration();
if (isset($configuration['providers']) && is_array($configuration['providers']) && count($configuration['providers']) > 0) {
$embedDescription .= " (" . implode(', ', $configuration['providers']) . ")";
}
}
$embedDescription .= "</li>";
}
}
}
$embedDescription .= "</ul>";
return FieldFilteredMarkup::create($embedDescription);
}
}