ValidFacebookInstantArticles.php in Facebook Instant Articles 3.x
File
modules/fb_instant_articles_views/src/Plugin/views/filter/ValidFacebookInstantArticles.php
View source
<?php
namespace Drupal\fb_instant_articles_views\Plugin\views\filter;
use Drupal\fb_instant_articles\Form\EntityViewDisplayEditForm;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ValidFacebookInstantArticles extends FilterPluginBase {
protected $entityTypeBundleInfo;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'));
}
public function adminSummary() {
return $this
->t('Filtering for items that have custom facebook view mode settings');
}
public function canExpose() {
return FALSE;
}
public function canBuildGroup() {
return FALSE;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['info'] = [
'#type' => 'markup',
'#markup' => 'Filter for nodes that implement the FIA view mode',
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
];
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->valueTitle = t('Facebook Instant Articles');
}
public function query() {
$this
->enabledNodeBundlesSetValues();
parent::query();
}
protected function enabledNodeBundlesSetValues() {
$entity_storage = $this->entityTypeManager
->getStorage('entity_view_display');
$node_types = [];
foreach ($this->entityTypeBundleInfo
->getBundleInfo('node') as $id => $bundle) {
$view_mode_id = 'node.' . $id . '.' . EntityViewDisplayEditForm::FBIA_VIEW_MODE;
$view_mode = $entity_storage
->load($view_mode_id);
if ($view_mode instanceof EntityViewDisplayInterface && $view_mode
->status()) {
$node_types[$id] = $id;
}
}
if (count($node_types) > 0) {
$this->value = $node_types;
$this->operator = 'IN';
}
}
}