HideIfAuthor.php in Field Formatter Condition 8
File
src/Plugin/Field/FieldFormatter/Condition/HideIfAuthor.php
View source
<?php
namespace Drupal\fico\Plugin\Field\FieldFormatter\Condition;
use Drupal\fico\Plugin\FieldFormatterConditionBase;
use Drupal\user\Entity\User;
class HideIfAuthor extends FieldFormatterConditionBase {
public function alterForm(&$form, $settings) {
if (isset($settings['settings']['author'])) {
$user = User::load($settings['settings']['author']);
}
else {
$user = NULL;
}
$config = \Drupal::config('user.settings');
$form['author'] = array(
'#title' => t('Authored by'),
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#selection_settings' => [
'include_anonymous' => FALSE,
],
'#description' => t('Leave blank for %anonymous.', [
'%anonymous' => $config
->get('anonymous'),
]),
'#default_value' => $user,
);
}
public function access(&$build, $field, $settings) {
$entity = $this
->getEntity($build);
if (!$entity) {
$build[$field]['#access'] = FALSE;
return;
}
if (!$settings['settings']['author'] && $entity
->getOwnerId() == 0 || $entity
->getOwnerId() == $settings['settings']['author']) {
$build[$field]['#access'] = FALSE;
}
}
public function summary($settings) {
if (isset($settings['settings']['author'])) {
$user = User::load($settings['settings']['author']);
$user = $user
->getUsername();
}
else {
$config = \Drupal::config('user.settings');
$user = $config
->get('anonymous');
}
return t("Condition: %condition (%settings)", [
"%condition" => t('Hide if content from author'),
'%settings' => $user,
]);
}
}