drupalorg-1.0.x-dev/src/OrganizationService.php
src/OrganizationService.php
<?php
namespace Drupal\drupalorg;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
/**
* Organization related helper methods.
*/
class OrganizationService {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected Connection $connection;
/**
* An instance of the entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Construct method.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager service.
*/
public function __construct(Connection $connection, EntityTypeManagerInterface $entityTypeManager) {
$this->connection = $connection;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Retrieve organization by title.
*
* @param string $title
* Organization title (unique).
*
* @return \Drupal\node\NodeInterface|null
* User entity or null.
*/
public function getOrganizationByTitle(string $title): ?NodeInterface {
$organization_id = $this->entityTypeManager->getStorage('node')->getQuery()
->accessCheck(FALSE)
->condition('type', 'organization')
->condition('title', $title)
->range(0, 1)
->execute();
$organization_id = !empty($organization_id) ? array_shift($organization_id) : NULL;
if ($organization_id) {
return $this->entityTypeManager->getStorage('node')->load($organization_id);
}
return NULL;
}
}
