/**
   * {@inheritdoc}
   */
  public function formElement(EntityInterface $entity) {
    $form = [];

    if ($this->entityTypeManager->getDefinition($entity->getEntityTypeId())->getKey('label')) {
      $form['label'] = array(
        '#type' => 'textfield',
        '#title' => $this->translationManager->translate('New Label'),
        '#maxlength' => 255,
        '#required' => TRUE,
      );
    }

    $form['id'] = array(
      '#type' => 'machine_name',
      '#title' => $this->translationManager->translate('New Id'),
      '#maxlength' => 255,
      '#required' => TRUE,
    );

    // If entity must have a prefix
    // (e.g. entity_form_mode, entity_view_mode, ...).
    if (method_exists($entity, 'getTargetType')) {
      $form['id']['#field_prefix'] = $entity->getTargetType() . '.';
    }

    if (method_exists($entity, 'load')) {
      $form['id']['#machine_name'] = [
        'exists' => get_class($entity) . '::load',
      ];
    }

    return $form;
  }
 /**
  * Builds a page listing all configuration keys to inspect.
  *
  * @return array
  *   A render array representing the list.
  */
 public function overview()
 {
     $page['#title'] = $this->t('Inspect');
     $page['#attached']['library'][] = 'system/drupal.system.modules';
     $page['filters'] = array('#type' => 'container', '#attributes' => array('class' => array('table-filter', 'js-show')));
     $page['filters']['text'] = array('#type' => 'search', '#title' => $this->t('Search'), '#size' => 30, '#placeholder' => $this->t('Search for a configuration key'), '#attributes' => array('class' => array('table-filter-text'), 'data-table' => '.config-inspector-list', 'autocomplete' => 'off', 'title' => $this->t('Enter a part of the configuration key to filter by.')));
     $page['table'] = array('#type' => 'table', '#header' => array('name' => t('Configuration key'), 'schema' => t('Schema'), 'list' => t('List'), 'tree' => t('Tree'), 'form' => t('Form'), 'raw' => t('Raw data')), '#attributes' => array('class' => array('config-inspector-list')));
     foreach ($this->storage->listAll() as $name) {
         $label = '<span class="table-filter-text-source">' . $name . '</span>';
         // Elements without a schema are displayed to help debugging.
         if (!$this->configInspectorManager->hasSchema($name)) {
             $page['table'][] = array('name' => array('#markup' => $label), 'schema' => array('#markup' => t('None')), 'list' => array('#markup' => t('N/A')), 'tree' => array('#markup' => t('N/A')), 'form' => array('#markup' => t('N/A')), 'raw' => array('#markup' => $this->l($this->t('Raw data'), new Url('config_inspector.raw_page', array('name' => $name)))));
         } else {
             $schema = t('Correct');
             $result = $this->configInspectorManager->checkValues($name);
             if (is_array($result)) {
                 // The no-schema case is covered above already, if we got errors, the
                 // schema is partial.
                 $schema = $this->translationManager->formatPlural(count($result), '@count error', '@count errors');
             }
             $page['table'][] = array('name' => array('#markup' => $label), 'schema' => array('#markup' => $schema), 'list' => array('#markup' => $this->l($this->t('List'), new Url('config_inspector.list_page', array('name' => $name)))), 'tree' => array('#markup' => $this->l($this->t('Tree'), new Url('config_inspector.tree_page', array('name' => $name)))), 'form' => array('#markup' => $this->l($this->t('Form'), new Url('config_inspector.form_page', array('name' => $name)))), 'raw' => array('#markup' => $this->l($this->t('Raw data'), new Url('config_inspector.raw_page', array('name' => $name)))));
         }
     }
     return $page;
 }
 /**
  * @dataProvider providerTestFormatPlural
  */
 public function testFormatPlural($count, $singular, $plural, array $args = array(), array $options = array(), $expected)
 {
     $translator = $this->getMock('\\Drupal\\Core\\StringTranslation\\Translator\\TranslatorInterface');
     $translator->expects($this->once())->method('getStringTranslation')->will($this->returnCallback(function ($langcode, $string) {
         return $string;
     }));
     $this->translationManager->addTranslator($translator);
     $result = $this->translationManager->formatPlural($count, $singular, $plural, $args, $options);
     $this->assertEquals($expected, $result);
 }
  /**
   * Returns an array of entity_clone permissions.
   *
   * @return array
   *   The permission list.
   */
  public function permissions() {
    $permissions = [];

    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      $permissions['clone ' . $entity_type_id . ' entity'] = $this->translationManager->translate('Clone all <em>@label</em> entities', [
        '@label' => $entity_type->getLabel(),
      ]);
    }

    return $permissions;
  }
示例#5
0
 /**
  * Dynamically set permissions for entity browsers with routes.
  */
 public function permissions()
 {
     $permissions = [];
     /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */
     $browsers = $this->browserStorage->loadMultiple();
     foreach ($browsers as $browser) {
         if ($browser->route()) {
             $permissions['access ' . $browser->id() . ' entity browser pages'] = array('title' => $this->translationManager->translate('Access @name pages', array('@name' => $browser->label())), 'description' => $this->translationManager->translate('Access pages that %browser uses to operate.', array('%browser' => $browser->label())));
         }
     }
     return $permissions;
 }
示例#6
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $entity_clone_handler */
    $entity_clone_handler = $this->entityTypeManager->getHandler($this->entityTypeDefinition->id(), 'entity_clone');
    if ($this->entityTypeManager->hasHandler($this->entityTypeDefinition->id(), 'entity_clone_form')) {
      $entity_clone_form_handler = $this->entityTypeManager->getHandler($this->entityTypeDefinition->id(), 'entity_clone_form');
    }

    $properties = [];
    if (isset($entity_clone_form_handler) && $entity_clone_form_handler) {
      $properties = $entity_clone_form_handler->getNewValues($form_state);
    }

    $cloned_entity = $entity_clone_handler->cloneEntity($this->entity, $this->entity->createDuplicate(), $properties);

    drupal_set_message($this->stringTranslationManager->translate('The entity <em>@entity (@entity_id)</em> of type <em>@type</em> was cloned', [
      '@entity' => $this->entity->label(),
      '@entity_id' => $this->entity->id(),
      '@type' => $this->entity->getEntityTypeId(),
    ]));

    if ($cloned_entity && $cloned_entity->hasLinkTemplate('canonical')) {
      $form_state->setRedirect($cloned_entity->toUrl()
        ->getRouteName(), $cloned_entity->toUrl()->getRouteParameters());
    }

    $form_state->setRedirect('<front>');
  }
示例#7
0
  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];

    /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      $has_clone_path = $entity_type->hasLinkTemplate('clone-form');
      $has_canonical_path = $entity_type->hasLinkTemplate('canonical');

      if ($has_clone_path) {
        $this->derivatives["$entity_type_id.clone_tab"] = array(
          'route_name' => "entity.$entity_type_id.clone_form",
          'title' => $this->translationManager->translate('Clone'),
          'base_route' => "entity.$entity_type_id." . ($has_canonical_path ? "canonical" : "edit_form"),
          'weight' => 100,
        );
      }
    }

    return $this->derivatives;
  }
示例#8
0
 /**
  * Tests translation using placeholders.
  *
  * @param string $string
  *   A string containing the English text to translate.
  * @param array $args
  *   An associative array of replacements to make after translation.
  * @param string $expected_string
  *   The expected translated string value.
  *
  * @dataProvider providerTestTranslatePlaceholder
  */
 public function testTranslatePlaceholder($string, array $args = array(), $expected_string)
 {
     $actual = $this->translationManager->translate($string, $args);
     $this->assertInstanceOf(MarkupInterface::class, $actual);
     $this->assertEquals($expected_string, (string) $actual);
 }