You are here

EntityViewDisplayEditForm.php in Facebook Instant Articles 3.x

Same filename and directory in other branches
  1. 8.2 src/Form/EntityViewDisplayEditForm.php

File

src/Form/EntityViewDisplayEditForm.php
View source
<?php

namespace Drupal\fb_instant_articles\Form;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\fb_instant_articles\Regions;
use Drupal\field_ui\Form\EntityViewDisplayEditForm as CoreEntityViewDisplayEditForm;

/**
 * Extends the core EntityViewDisplayEditForm to support multiple regions.
 */
class EntityViewDisplayEditForm extends CoreEntityViewDisplayEditForm {

  /**
   * Name of the FBIA view mode.
   */
  const FBIA_VIEW_MODE = 'fb_instant_articles';

  /**
   * {@inheritdoc}
   */
  public function getRegions() {
    $regions = parent::getRegions();
    if ($this
      ->getEntity()
      ->getOriginalMode() === 'fb_instant_articles') {
      $new_regions[Regions::REGION_HEADER] = [
        'title' => $this
          ->t('Header'),
        'message' => $this
          ->t('No fields are displayed in this region.'),
      ];
      $new_regions[Regions::REGION_CONTENT] = [
        'title' => $this
          ->t('Body'),
        'message' => $this
          ->t('No fields are displayed in this region.'),
      ];
      $new_regions[Regions::REGION_FOOTER] = [
        'title' => $this
          ->t('Footer'),
        'message' => $this
          ->t('No fields are displayed in this region.'),
      ];
      $new_regions['hidden'] = $regions['hidden'];
      $regions = $new_regions;
    }
    return $regions;
  }

  /**
   * {@inheritdoc}
   */
  protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
    $options = parent::getApplicablePluginOptions($field_definition);

    // Filter out FBIA formatters for view modes other than the Facebook instant
    // articles view mode.
    if ($this
      ->getEntity()
      ->getOriginalMode() !== 'fb_instant_articles') {
      foreach ($options as $key => $label) {
        if (preg_match('/^fbia_/', $key)) {
          unset($options[$key]);
        }
      }
    }
    return $options;
  }

}

Classes

Namesort descending Description
EntityViewDisplayEditForm Extends the core EntityViewDisplayEditForm to support multiple regions.