og-8.x-1.x-dev/src/Plugin/OgFields/AudienceField.php

src/Plugin/OgFields/AudienceField.php
<?php

declare(strict_types=1);

namespace Drupal\og\Plugin\OgFields;

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\Attribute\OgFields;
use Drupal\og\OgFieldBase;
use Drupal\og\OgFieldsInterface;
use Drupal\og\OgGroupAudienceHelperInterface;

/**
 * Determine to which groups this group content is assigned to.
 */
#[OgFields(
  id: 'og_audience',
  type: 'group',
  description: new TranslatableMarkup('Determine to which groups this group content is assigned to.'),
)]
class AudienceField extends OgFieldBase implements OgFieldsInterface {

  /**
   * {@inheritdoc}
   */
  public function getFieldStorageBaseDefinition(array $values = []): array {
    if ($this->getEntityType() === 'user') {
      throw new \LogicException('OG audience field cannot be added to the User entity type.');
    }

    $values += [
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => [
        'target_type' => $this->getEntityType(),
      ],
      'type' => OgGroupAudienceHelperInterface::GROUP_REFERENCE,
    ];

    return parent::getFieldStorageBaseDefinition($values);
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldBaseDefinition(array $values = []): array {
    $values += [
      'description' => $this->t('OG group audience reference field.'),
      'display_label' => TRUE,
      'label' => $this->t('Groups audience'),
      'settings' => [
        'handler' => 'og',
        'handler_settings' => [],
      ],
    ];

    return parent::getFieldBaseDefinition($values);
  }

  /**
   * {@inheritdoc}
   */
  public function getFormDisplayDefinition(array $values = []): array {
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function getViewDisplayDefinition(array $values = []): array {
    $values += [
      'label' => 'above',
      'type' => 'entity_reference_label',
      'settings' => [
        'link' => TRUE,
      ],
    ];

    return $values;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc