/** * Tests the functionality of test actions. */ public function testOperations() { // Test that actions can be discovered. $definitions = $this->actionManager->getDefinitions(); $this->assertTrue(count($definitions) > 1, 'Action definitions are found.'); $this->assertTrue(!empty($definitions['action_test_no_type']), 'The test action is among the definitions found.'); $definition = $this->actionManager->getDefinition('action_test_no_type'); $this->assertTrue(!empty($definition), 'The test action definition is found.'); $definitions = $this->actionManager->getDefinitionsByType('user'); $this->assertTrue(empty($definitions['action_test_no_type']), 'An action with no type is not found.'); // Create an instance of the 'save entity' action. $action = $this->actionManager->createInstance('action_test_save_entity'); $this->assertTrue($action instanceof ActionInterface, 'The action implements the correct interface.'); // Create a new unsaved user. $name = $this->randomMachineName(); $user_storage = $this->container->get('entity.manager')->getStorage('user'); $account = $user_storage->create(array('name' => $name, 'bundle' => 'user')); $loaded_accounts = $user_storage->loadMultiple(); $this->assertEqual(count($loaded_accounts), 0); // Execute the 'save entity' action. $action->execute($account); $loaded_accounts = $user_storage->loadMultiple(); $this->assertEqual(count($loaded_accounts), 1); $account = reset($loaded_accounts); $this->assertEqual($name, $account->label()); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $actions = array(); foreach ($this->manager->getDefinitions() as $id => $definition) { if (is_subclass_of($definition['class'], '\\Drupal\\Core\\Plugin\\PluginFormInterface')) { $key = Crypt::hashBase64($id); $actions[$key] = $definition['label'] . '...'; } } $form['parent'] = array('#type' => 'details', '#title' => $this->t('Create an advanced action'), '#attributes' => array('class' => array('container-inline')), '#open' => TRUE); $form['parent']['action'] = array('#type' => 'select', '#title' => $this->t('Action'), '#title_display' => 'invisible', '#options' => $actions, '#empty_option' => $this->t('Choose an advanced action')); $form['parent']['actions'] = array('#type' => 'actions'); $form['parent']['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Create')); return $form; }
/** * {@inheritdoc} * * @param string $action_id * The hashed version of the action ID. */ public function buildForm(array $form, FormStateInterface $form_state, $action_id = NULL) { // In \Drupal\action\Form\ActionAdminManageForm::buildForm() the action // are hashed. Here we have to decrypt it to find the desired action ID. foreach ($this->actionManager->getDefinitions() as $id => $definition) { $key = Crypt::hashBase64($id); if ($key === $action_id) { $this->entity->setPlugin($id); // Derive the label and type from the action definition. $this->entity->set('label', $definition['label']); $this->entity->set('type', $definition['type']); break; } } return parent::buildForm($form, $form_state); }