Skip to content
Snippets Groups Projects

Add enum for project type

Files

+ 19
5
@@ -6,6 +6,7 @@ use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Url;
use Drupal\project_browser\ProjectType;
/**
* Defines a single Project.
@@ -28,6 +29,13 @@ class Project implements \JsonSerializable {
*/
public string|Url|null $commands;
/**
* The project type (e.g., module, theme, recipe, or something else).
*
* @var \Drupal\project_browser\ProjectType|string
*/
public readonly ProjectType|string $type;
/**
* Constructs a Project object.
*
@@ -69,10 +77,9 @@ class Project implements \JsonSerializable {
* Images of the project.
* @param array $warnings
* Warnings for the project.
* @param string $type
* The project type. Defaults to 'module:drupalorg' to indicate modules from
* D.O., but may be changed to anything else that could helpfully identify
* a project type.
* @param string|ProjectType $type
* The project type. Defaults to a module, but may be any string that is not
* one of the cases of \Drupal\project_browser\ProjectType.
* @param string $id
* (optional) The unqualified project ID. Cannot contain a slash. Defaults
* to the machine name.
@@ -97,10 +104,17 @@ class Project implements \JsonSerializable {
public array $categories = [],
public array $images = [],
public array $warnings = [],
public readonly string $type = 'module:drupalorg',
string|ProjectType $type = ProjectType::Module,
string $id = '',
) {
$this->setSummary($body);
if (is_string($type)) {
// If the $type can't be mapped to a ProjectType case, use it as-is.
$type = ProjectType::tryFrom($type) ?? $type;
}
$this->type = $type;
// @see \Drupal\project_browser\ProjectBrowser\ProjectsResultsPage::jsonSerialize()
// @see \Drupal\project_browser\Routing\ProjectBrowserRoutes::routes()
if (str_contains($id, '/')) {
Loading