Skip to content

Instantly share code, notes, and snippets.

@ZombiMorgan
Last active October 24, 2020 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZombiMorgan/df6f6c3aa972d4d9d7610942d04505b5 to your computer and use it in GitHub Desktop.
Save ZombiMorgan/df6f6c3aa972d4d9d7610942d04505b5 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\projects\Plugin\Block;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an example block.
*
* @Block(
* id = "button_block",
* admin_label = @Translation("Button Block"),
* category = @Translation("Custom")
* )
*/
class ButtonBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $currentUser;
protected $routeMatch;
protected $entityManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container->get('current_user');
$instance->routeMatch = $container->get('current_route_match');
$instance->entityManager = $container->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function build() {
$button = [];
$project = $this->routeMatch->getParameter('node');
if ($this->currentUser->hasPermission('administer projects configuration') && $project) {
$button['container'] = [
'#type' => 'actions',
'#title' => $this->t('Actions for project "@project"', ['@project' => $project->label()])
];
switch ($project->field_status->value) {
case 0:
$button['container']['start'] = [
'#type' => 'submit',
'#value' => $this->t('Start project'),
'#submit' => [get_class($this), 'startProject'],
'#attributes' => [
'class' => ['btn', 'btn-success'],
],
];
break;
case 1:
$button['container']['profit'] = [
'#type' => 'link',
'#title' => $this->t('Profit'),
'#attributes' => [
'class' => ['use-ajax', 'btn', 'btn-primary'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 'auto',
'maxWidth' => 600,
'fluid' => true,
]),
],
'#url' => \Drupal\Core\Url::fromRoute('entity.node.project_profit_form', ['node' => $project->id()]),
'#attached' => [
'library' => ['core/drupal.dialog.ajax'],
],
];
$button['container']['stop'] = [
'#type' => 'submit',
'#value' => $this->t('Close project'),
'#submit' => [get_class($this), 'closeProject'],
'#attributes' => [
'class' => ['btn', 'btn-danger'],
],
];
break;
case 5:
$button = [];
}
return $button;
}
return [];
}
function startProject() {
dpm(get_class($this));
}
function closeProject() {
dpm(get_class($this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment