Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function actions(array $form, FormStateInterface $form_state)
 {
     $actions = parent::actions($form, $form_state);
     $actions['submit']['#value'] = $this->t('Save settings');
     if (!$this->entity->isNew()) {
         $target_entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
         $route_parameters = ['field_config' => $this->entity->id()] + FieldUI::getRouteBundleParameter($target_entity_type, $this->entity->bundle);
         $url = new Url('entity.field_config.' . $target_entity_type->id() . '_field_delete_form', $route_parameters);
         if ($this->getRequest()->query->has('destination')) {
             $query = $url->getOption('query');
             $query['destination'] = $this->getRequest()->query->get('destination');
             $url->setOption('query', $query);
         }
         $actions['delete'] = array('#type' => 'link', '#title' => $this->t('Delete'), '#url' => $url, '#access' => $this->entity->access('delete'), '#attributes' => array('class' => array('button', 'button--danger')));
     }
     return $actions;
 }
Ejemplo n.º 2
0
 /**
  * Creates an items list for the given properties.
  *
  * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $properties
  *   The property definitions, keyed by their property names.
  * @param string $active_property_path
  *   The relative property path to the active property.
  * @param \Drupal\Core\Url $base_url
  *   The base URL to which property path parameters should be added for
  *   the navigation links.
  * @param string $parent_path
  *   (optional) The common property path prefix of the given properties.
  * @param string $label_prefix
  *   (optional) The prefix to use for the labels of created fields.
  *
  * @return array
  *   A render array representing the given properties and, possibly, nested
  *   properties.
  */
 protected function getPropertiesList(array $properties, $active_property_path, Url $base_url, $parent_path = '', $label_prefix = '')
 {
     $list = array('#theme' => 'search_api_form_item_list');
     $active_item = '';
     if ($active_property_path) {
         list($active_item, $active_property_path) = explode(':', $active_property_path, 2) + array(1 => '');
     }
     $type_mapping = Utility::getFieldTypeMapping();
     $query_base = $base_url->getOption('query');
     foreach ($properties as $key => $property) {
         $this_path = $parent_path ? $parent_path . ':' : '';
         $this_path .= $key;
         $label = $property->getLabel();
         $property = Utility::getInnerProperty($property);
         $can_be_indexed = TRUE;
         $nested_properties = array();
         $parent_child_type = NULL;
         if ($property instanceof ComplexDataDefinitionInterface) {
             $can_be_indexed = FALSE;
             $nested_properties = $property->getPropertyDefinitions();
             $main_property = $property->getMainPropertyName();
             if ($main_property && isset($nested_properties[$main_property])) {
                 $parent_child_type = $property->getDataType() . '.';
                 $property = $nested_properties[$main_property];
                 $parent_child_type .= $property->getDataType();
                 unset($nested_properties[$main_property]);
                 $can_be_indexed = TRUE;
             }
             // Don't add the additional 'entity' property for entity reference
             // fields which don't target a content entity type.
             if ($property instanceof FieldItemDataDefinition && in_array($property->getDataType(), array('field_item:entity_reference', 'field_item:image', 'field_item:file'))) {
                 $entity_type = $this->getEntityTypeManager()->getDefinition($property->getSetting('target_type'));
                 if (!$entity_type->isSubclassOf('Drupal\\Core\\Entity\\ContentEntityInterface')) {
                     unset($nested_properties['entity']);
                 }
             }
         }
         // Don't allow indexing of properties with unmapped types. Also, prefer
         // a "parent.child" type mapping (taking into account the parent property
         // for, e.g., text fields).
         $type = $property->getDataType();
         if ($parent_child_type && !empty($type_mapping[$parent_child_type])) {
             $type = $parent_child_type;
         } elseif (empty($type_mapping[$type])) {
             // Remember the type only if it was not explicitly mapped to FALSE.
             if (!isset($type_mapping[$type])) {
                 $this->unmappedFields[$type][] = $label_prefix . $label;
             }
             $can_be_indexed = FALSE;
         }
         // If the property can neither be expanded nor indexed, just skip it.
         if (!($nested_properties || $can_be_indexed)) {
             continue;
         }
         $nested_list = array();
         $expand_link = array();
         if ($nested_properties) {
             if ($key == $active_item) {
                 $link_url = clone $base_url;
                 $query_base['property_path'] = $parent_path;
                 $link_url->setOption('query', $query_base);
                 $expand_link = array('#type' => 'link', '#title' => '(-) ', '#url' => $link_url);
                 $nested_list = $this->getPropertiesList($nested_properties, $active_property_path, $base_url, $this_path, $label_prefix . $label . ' » ');
             } else {
                 $link_url = clone $base_url;
                 $query_base['property_path'] = $this_path;
                 $link_url->setOption('query', $query_base);
                 $expand_link = array('#type' => 'link', '#title' => '(+) ', '#url' => $link_url);
             }
         }
         $item = array('#type' => 'container', '#attributes' => array('class' => array('container-inline')));
         if ($expand_link) {
             $item['expand_link'] = $expand_link;
         }
         $item['label']['#markup'] = Html::escape($label) . ' ';
         if ($can_be_indexed) {
             $item['add'] = array('#type' => 'submit', '#name' => Utility::createCombinedId($this->getParameter('datasource') ?: NULL, $this_path), '#value' => $this->t('Add'), '#submit' => array('::addField', '::save'), '#property' => $property, '#prefixed_label' => $label_prefix . $label, '#data_type' => $type_mapping[$type]);
         }
         if ($nested_list) {
             $item['properties'] = $nested_list;
         }
         $list[] = $item;
     }
     return $list;
 }
Ejemplo n.º 3
0
 /**
  * Asserts that a given URL object matches the expectations.
  *
  * @param string $expected_route_name
  *   The expected route name of the generated URL.
  * @param array $expected_route_parameters
  *   The expected route parameters of the generated URL.
  * @param \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject $entity
  *   The entity that is expected to be set as a URL option.
  * @param bool $has_language
  *   Whether or not the URL is expected to have a language option.
  * @param \Drupal\Core\Url $url
  *   The URL option to make the assertions on.
  */
 protected function assertUrl($expected_route_name, array $expected_route_parameters, $entity, $has_language, Url $url)
 {
     $this->assertEquals($expected_route_name, $url->getRouteName());
     $this->assertEquals($expected_route_parameters, $url->getRouteParameters());
     $this->assertEquals($this->entityTypeId, $url->getOption('entity_type'));
     $this->assertEquals($entity, $url->getOption('entity'));
     if ($has_language) {
         $this->assertEquals($this->langcode, $url->getOption('language')->getId());
     } else {
         $this->assertNull($url->getOption('language'));
     }
 }
  /**
   * {@inheritDoc}
   */
  public function parseLink(Url $link) {
    $query = $link->getOption('query');

    if (!$query) {
      throw new \InvalidArgumentException('The \Drupal\Core\Url you pass in must
      have its \'query\' option set.');
    }

    $request_context = [
      'entity_ids' => NULL,
      'fid' => NULL,
      'sample' => NULL,
      'force_download' => FALSE,
      'flatten' => TRUE,
    ];

    if (!empty($query['sample'])) {
      $sample = TRUE;
    }

    // Is this just the PDF populated with sample data?
    $request_context['sample'] = $sample;

    if (!empty($query['fid'])) {
      $request_context['fid'] = $query['fid'];
    }
    else {
      throw new \InvalidArgumentException('fid parameter missing from query
      string; cannot determine how to proceed, so failing.');
    }

    if (!empty($query['entity_type'])) {
      $request_context['entity_type'] = $query['entity_type'];
    }

    $request_context['entity_ids'] = $entity_ids = [];
    if (!empty($query['entity_id']) || !empty($query['entity_ids'])) {
      $entity_ids = (!empty($query['entity_id']) ? [$query['entity_id']] : $query['entity_ids']);

      // Re-key entity IDs so they can be loaded easily with loadMultiple().
      // If we have type information, add it to the types array, and remove it
      // in order to make sure we only store the ID in the entity_ids key.
      foreach ($entity_ids as $entity_id) {
        $entity_id_parts = explode(':', $entity_id);

        if (count($entity_id_parts) == 2) {
          $entity_type = $entity_id_parts[0];
          $entity_id = $entity_id_parts[1];
        }
        elseif (!empty($request_context['entity_type'])) {
          $entity_type = $request_context['entity_type'];
        }
        else {
          $entity_type = 'node';
        }
        $request_context['entity_ids'] += [
          $entity_type => [],
        ];

        $request_context['entity_ids'][$entity_type][$entity_id] = $entity_id;
      }
    }
    else {
      // Populate defaults.
      $fillpdf_form = FillPdfForm::load($request_context['fid']);
      $default_entity_id = $fillpdf_form->default_entity_id->value;
      if ($default_entity_id) {
        $default_entity_type = $fillpdf_form->default_entity_type->value;
        if (empty($default_entity_type)) {
          $default_entity_type = 'node';
        }

        $request_context['entity_ids'] = [
          $default_entity_type => [$default_entity_id => $default_entity_id],
        ];
      }
    }

    // We've processed the shorthand forms, so unset them.
    unset($request_context['entity_id'], $request_context['entity_type']);

    if (!$query['download'] && (int) $query['download'] == 1) {
      $request_context['force_download'] = TRUE;
    }
    if ($query['flatten'] && (int) $query['flatten'] == 0) {
      $request_context['flatten'] = FALSE;
    }

    return $request_context;
  }