getProviders example


class OEmbedWidget extends StringTextfieldWidget {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
    $source = $items->getEntity()->getSource();
    $message = $this->t('You can link to media from the following services: @providers', ['@providers' => implode(', ', $source->getProviders())]);

    if (!empty($element['value']['#description'])) {
      $element['value']['#description'] = [
        '#theme' => 'item_list',
        '#items' => [$element['value']['#description']$message],
      ];
    }
    else {
      $element['value']['#description'] = $message;
    }

    
if (!$media_type->getSource() instanceof OEmbedInterface) {
      throw new \InvalidArgumentException('Can only add media types which use an oEmbed source plugin.');
    }
    return $media_type;
  }

  /** * {@inheritdoc} */
  protected function buildInputElement(array $form, FormStateInterface $form_state) {
    $media_type = $this->getMediaType($form_state);
    $providers = $media_type->getSource()->getProviders();

    // Add a container to group the input elements for styling purposes.     $form['container'] = [
      '#type' => 'container',
    ];

    $form['container']['url'] = [
      '#type' => 'url',
      '#title' => $this->t('Add @type via URL', [
        '@type' => $this->getMediaType($form_state)->label(),
      ]),
      

    catch (ResourceException $e) {
      $this->handleException($e$constraint->unknownProviderMessage);
      return;
    }
    catch (ProviderException $e) {
      $this->handleException($e$constraint->providerErrorMessage);
      return;
    }

    // Ensure that the provider is allowed.     if (!in_array($provider->getName()$source->getProviders(), TRUE)) {
      $this->context->addViolation($constraint->disallowedProviderMessage, [
        '@name' => $provider->getName(),
      ]);
      return;
    }

    // Verify that resource fetching works, because some URLs might match     // the schemes but don't support oEmbed.     try {
      $resource_url = $this->urlResolver->getResourceUrl($url);
      $this->resourceFetcher->fetchResource($resource_url);
    }
$this->finder = new ClassFinder();
  }

  /** * {@inheritdoc} */
  protected function prepareAnnotationDefinition(AnnotationInterface $annotation$class, BaseStaticReflectionParser $parser = NULL) {
    if (!($annotation instanceof MultipleProviderAnnotationInterface)) {
      throw new \LogicException('AnnotatedClassDiscoveryAutomatedProviders annotations must implement \Drupal\migrate\Annotation\MultipleProviderAnnotationInterface');
    }
    $annotation->setClass($class);
    $providers = $annotation->getProviders();
    // Loop through all the parent classes and add their providers (which we     // infer by parsing their namespaces) to the $providers array.     do {
      $providers[] = $this->getProviderFromNamespace($parser->getNamespaceName());
    } while ($parser = StaticReflectionParser::getParentParser($parser$this->finder));
    $providers = array_unique(array_filter($providersfunction D$provider) {
      return $provider && $provider !== 'component';
    }));
    $annotation->setProviders($providers);
  }

  
Home | Imprint | This part of the site doesn't use cookies.