Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ Allows to reference and print a custom block plugin. You only need to put `allow
*/
~~~~

Version 4 supports PHP Attributes:
~~~~
#[Block(
id: "category_products_block",
admin_label: new TranslatableMarkup("Category products"),
context_definitions: [
'allow_as_block_plugin_field_context' => new ContextDefinition('boolean'),
]
)]
~~~~

### Mr. Milú Color:
Provides a field type to store a color in hexadecimal format.

Expand Down
4 changes: 2 additions & 2 deletions mrmilu_fields.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name: Mr. Milú Fields
description: Provides some custom fields
package: Mr. Milú
type: module
core_version_requirement: ^8.8.0 || ^9 || ^10
version: 3.0.0
core_version_requirement: ^10 || ^11
version: 4.0.0
5 changes: 5 additions & 0 deletions mrmilu_fields.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
mrmilu_fields.allow_as_plugin_field_context:
class: Drupal\mrmilu_fields\ContextProvider\AllowAsBlockPluginFieldContext
tags:
- { name: 'context_provider' }
30 changes: 30 additions & 0 deletions src/ContextProvider/AllowAsBlockPluginFieldContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\mrmilu_fields\ContextProvider;

use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextProviderInterface;

/**
* Defines a class to allow block plugin be referenced in field.
*/
class AllowAsBlockPluginFieldContext implements ContextProviderInterface {

/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids): array {
return [
'allow_as_block_plugin_field_context' => new Context(new ContextDefinition('boolean'), TRUE),
];
}

/**
* {@inheritdoc}
*/
public function getAvailableContexts(): array {
return $this->getRuntimeContexts([]);
}

}
6 changes: 6 additions & 0 deletions src/Plugin/Field/FieldWidget/MrMiluBlockPluginWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ public function getOptions() {
$options = ['_none' => t('- None -')];
$definitions = $this->blockManager->getDefinitionsForContexts($this->contextRepository->getAvailableContexts());
foreach ($definitions as $id => $definition) {
// Default support for Annotations.
if (!empty($definition['allow_as_block_plugin_field'])) {
$options[$id] = $definition['admin_label'];
continue;
}
// Support for PHP Attributes.
if (isset($definition['context_definitions']['allow_as_block_plugin_field_context'])) {
$options[$id] = $definition['admin_label'];
}
}
return $options;
Expand Down