/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, PageInterface $page = NULL, $name = '') { $this->page = $page; $this->staticContext = $this->page->getStaticContext($name); // Allow the condition to add to the form. $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => isset($this->staticContext['label']) ? $this->staticContext['label'] : '', '#required' => TRUE]; $form['machine_name'] = ['#type' => 'machine_name', '#maxlength' => 64, '#required' => TRUE, '#machine_name' => ['exists' => [$this, 'contextExists'], 'source' => ['label']], '#default_value' => $name]; $form['entity_type'] = ['#type' => 'select', '#title' => $this->t('Entity type'), '#options' => $this->entityTypeRepository->getEntityTypeLabels(TRUE), '#limit_validation_errors' => array(array('entity_type')), '#submit' => ['::rebuildSubmit'], '#executes_submit_callback' => TRUE, '#ajax' => array('callback' => '::updateEntityType', 'wrapper' => 'add-static-context-wrapper', 'method' => 'replace')]; $entity = NULL; if ($form_state->hasValue('entity_type')) { $entity_type = $form_state->getValue('entity_type'); if ($this->staticContext['value']) { $entity = $this->entityRepository->loadEntityByUuid($entity_type, $this->staticContext['value']); } } elseif (!empty($this->staticContext['type'])) { list(, $entity_type) = explode(':', $this->staticContext['type']); $entity = $this->entityRepository->loadEntityByUuid($entity_type, $this->staticContext['value']); } elseif ($this->entityTypeManager->hasDefinition('node')) { $entity_type = 'node'; } else { $entity_type = 'user'; } $form['entity_type']['#default_value'] = $entity_type; $form['selection'] = ['#type' => 'entity_autocomplete', '#prefix' => '<div id="add-static-context-wrapper">', '#suffix' => '</div>', '#required' => TRUE, '#target_type' => $entity_type, '#default_value' => $entity, '#title' => $this->t('Select entity')]; $form['actions'] = ['#type' => 'actions']; $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->submitButtonText(), '#button_type' => 'primary']; return $form; }
/** * {@inheritDoc} */ public function buildEntity(array $form, FormStateInterface $form_state) { /** @var \Drupal\pathauto\PathautoPatternInterface $entity */ $entity = parent::buildEntity($form, $form_state); $default_weight = 0; $alias_type = $entity->getAliasType(); if ($alias_type->getDerivativeId() && $this->entityTypeManager->hasDefinition($alias_type->getDerivativeId())) { $entity_type = $alias_type->getDerivativeId(); // First, remove bundle and language conditions. foreach ($entity->getSelectionConditions() as $condition_id => $condition) { if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type, 'node_type', 'language'])) { $entity->removeSelectionCondition($condition_id); } } if ($bundles = array_filter((array) $form_state->getValue('bundles'))) { $default_weight -= 5; $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type; $entity->addSelectionCondition(['id' => $plugin_id, 'bundles' => $bundles, 'negate' => FALSE, 'context_mapping' => [$entity_type => $entity_type]]); } if ($languages = array_filter((array) $form_state->getValue('languages'))) { $default_weight -= 5; $language_mapping = $entity_type . ':' . $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode') . ':language'; $entity->addSelectionCondition(['id' => 'language', 'langcodes' => array_combine($languages, $languages), 'negate' => FALSE, 'context_mapping' => ['language' => $language_mapping]]); $entity->addRelationship($language_mapping, t('Language')); } } $entity->setWeight($default_weight); return $entity; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $embed_button = $form_state->getTemporaryValue('embed_button'); $entity_type_id = $this->getConfigurationValue('entity_type'); $form['entity_type'] = array('#type' => 'select', '#title' => $this->t('Entity type'), '#options' => $this->getEntityTypeOptions(), '#default_value' => $entity_type_id, '#description' => $this->t("Entity type for which this button is to enabled."), '#required' => TRUE, '#ajax' => array('callback' => array($form_state->getFormObject(), 'updateTypeSettings'), 'effect' => 'fade'), '#disabled' => !$embed_button->isNew()); if ($entity_type_id) { $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $form['bundles'] = array('#type' => 'checkboxes', '#title' => $entity_type->getBundleLabel() ?: $this->t('Bundles'), '#options' => $this->getEntityBundleOptions($entity_type), '#default_value' => $this->getConfigurationValue('bundles'), '#description' => $this->t('If none are selected, all are allowed.')); $form['bundles']['#access'] = !empty($form['bundles']['#options']); // Allow option to limit Entity Embed Display plugins. $form['display_plugins'] = array('#type' => 'checkboxes', '#title' => $this->t('Allowed Entity Embed Display plugins'), '#options' => $this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id), '#default_value' => $this->getConfigurationValue('display_plugins'), '#description' => $this->t('If none are selected, all are allowed. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.')); $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']); /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */ if ($this->entityTypeManager->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager->getStorage('entity_browser')->loadMultiple())) { $ids = array_keys($browsers); $labels = array_map(function ($item) { /** @var \Drupal\entity_browser\EntityBrowserInterface $item */ return $item->label(); }, $browsers); $options = ['_none' => $this->t('None (autocomplete)')] + array_combine($ids, $labels); $form['entity_browser'] = ['#type' => 'select', '#title' => $this->t('Entity browser'), '#description' => $this->t('Entity browser to be used to select entities to be embedded.'), '#options' => $options, '#default_value' => $this->getConfigurationValue('entity_browser')]; $form['entity_browser_settings'] = ['#type' => 'details', '#title' => $this->t('Entity browser settings'), '#open' => TRUE, '#states' => ['invisible' => [':input[name="type_settings[entity_browser]"]' => ['value' => '_none']]]]; $form['entity_browser_settings']['display_review'] = ['#type' => 'checkbox', '#title' => 'Display the entity after selection', '#default_value' => $this->getConfigurationValue('entity_browser_settings')['display_review']]; } else { $form['entity_browser'] = ['#type' => 'value', '#value' => '']; } } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) { $this->entityTypeId = $entity_type_id; if (!$this->entityTypeManager->hasDefinition($this->entityTypeId)) { throw new NotFoundHttpException(); } $form = parent::buildForm($form, $form_state); $storage = $this->entityTypeManager->getStorage($entity_type_id); $count = $storage->getQuery()->count()->execute(); $form['entity_type_id'] = ['#type' => 'value', '#value' => $entity_type_id]; // Display a list of the 10 entity labels, if possible. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); if ($count == 0) { $form['total'] = ['#markup' => $this->t('There are 0 @entity_type_plural to delete.', ['@entity_type_plural' => $entity_type->getPluralLabel()])]; } elseif ($entity_type->hasKey('label')) { $recent_entity_ids = $storage->getQuery()->sort($entity_type->getKey('id'), 'DESC')->pager(10)->execute(); $recent_entities = $storage->loadMultiple($recent_entity_ids); $labels = []; foreach ($recent_entities as $entity) { $labels[] = $entity->label(); } if ($labels) { $form['recent_entity_labels'] = ['#theme' => 'item_list', '#items' => $labels]; $more_count = $count - count($labels); $form['total'] = ['#markup' => $this->formatPlural($more_count, 'And <strong>@count</strong> more @entity_type_singular.', 'And <strong>@count</strong> more @entity_type_plural.', ['@entity_type_singular' => $entity_type->getSingularLabel(), '@entity_type_plural' => $entity_type->getPluralLabel()]), '#access' => (bool) $more_count]; } } else { $form['total'] = ['#markup' => $this->formatPlural($count, 'This will delete <strong>@count</strong> @entity_type_singular.', 'This will delete <strong>@count</strong> @entity_type_plural.', ['@entity_type_singular' => $entity_type->getSingularLabel(), '@entity_type_plural' => $entity_type->getPluralLabel()])]; } $form['description']['#prefix'] = '<p>'; $form['description']['#suffix'] = '</p>'; $form['description']['#weight'] = 5; // Only show the delete button if there are entities to delete. $form['actions']['submit']['#access'] = (bool) $count; return $form; }
/** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { $tempstore_id = !empty($definition['tempstore_id']) ? $definition['tempstore_id'] : $defaults['tempstore_id']; $machine_name = $this->convertVariable($value, $defaults); list(, $parts) = explode(':', $definition['type'], 2); $parts = explode(':', $parts); foreach ($parts as $key => $part) { $parts[$key] = $this->convertVariable($part, $defaults); } $cached_values = $this->tempstore->get($tempstore_id)->get($machine_name); // Entity type upcasting is most common, so we just assume that here. // @todo see if there's a better way to do this. if (!$cached_values && $this->entityTypeManager->hasDefinition($name)) { $value = $this->entityTypeManager->getStorage($name)->load($machine_name); return $value; } elseif (!$cached_values) { return NULL; } else { $value = NestedArray::getValue($cached_values, $parts, $key_exists); return $key_exists ? $value : NULL; } }