og-8.x-1.x-dev/src/OgFieldBase.php
src/OgFieldBase.php
<?php
declare(strict_types=1);
namespace Drupal\og;
use Drupal\Core\Plugin\PluginBase;
/**
* OG related fields base plugin.
*/
abstract class OgFieldBase extends PluginBase implements OgFieldsInterface {
/**
* The entity bundle.
*/
protected string $bundle;
/**
* The entity type.
*/
protected string $entityType = '';
/**
* The field name.
*/
protected string $fieldName;
/**
* {@inheritdoc}
*/
public function setBundle(string $bundle): static {
$this->bundle = $bundle;
return $this;
}
/**
* {@inheritdoc}
*/
public function getBundle(): string {
return $this->bundle;
}
/**
* {@inheritdoc}
*/
public function getEntityType(): string {
return $this->entityType;
}
/**
* {@inheritdoc}
*/
public function setEntityType(string $entity_type): static {
$field_storage = $this->getFieldStorageBaseDefinition();
if (!empty($field_storage['entity']) && !in_array($entity_type, $field_storage['entity'])) {
$plugin_id = $this->getPluginId();
$entities = implode(', ', $field_storage['entity']);
if ($field_name = $this->getFieldName()) {
throw new \Exception("The Organic Groups field with plugin ID $plugin_id with the name $field_name cannot be attached to the entity type. It can only be attached to the following entities: $entities.");
}
throw new \Exception("The Organic Groups field with plugin ID $plugin_id cannot be attached to the entity type. It can only be attached to the following entities: $entities.");
}
$this->entityType = $entity_type;
return $this;
}
/**
* {@inheritdoc}
*/
public function getFieldName(): string {
return $this->fieldName;
}
/**
* {@inheritdoc}
*/
public function setFieldName(string $field_name): static {
$this->fieldName = $field_name;
return $this;
}
/**
* {@inheritdoc}
*/
public function getFieldStorageBaseDefinition(array $values = []): array {
$values += [
'entity_type' => $this->getEntityType(),
'field_name' => $this->getFieldName(),
];
return $values;
}
/**
* {@inheritdoc}
*/
public function getFieldBaseDefinition(array $values = []): array {
$values += [
'bundle' => $this->getBundle(),
'entity_type' => $this->getEntityType(),
'field_name' => $this->getFieldName(),
];
return $values;
}
}
