public static function saveExpo_exist($d, $nid) { $node = node_load($nid); $node->setTitle($d['title']); $node->field_authoremail->setValue($d['email']); $node->field_author_plain->setValue($d['author']); $node->field_public->setValue($d['public']); //tag field_tag Entity reference if (is_array($d['body'])) { $body_value = ['format' => 'rich_html_base', 'value' => $d['body']['value']]; } else { $body_value = ['format' => 'rich_html_base', 'value' => $d['body']]; } //if(is_array($d['body'])) $body_value = $d['body']['value']; //else $body_value = $d['body']; $node->field_body->setValue($body_value); if ($d['featuredimage']) { if (is_array($d['featuredimage'])) { $target_id = $d['featuredimage']['target_id']; if ($node->field_featuredimage) { $node->field_featuredimage->setValue(['target_id' => $target_id]); } } } $node->save(); $fcbundle = 'field_collitem'; $cnt = count($node->{$fcbundle}); for ($i = 0; $i < $cnt; $i++) { $fcid = $node->field_collitem[$i]->value; $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $fcitem->delete(); } $node->{$fcbundle} = []; //foreach ($node->{$fcbundle} as $key => $value) { // unset($node->{$fcbundle}[$key]); //} //$node->save(); foreach ($d['collitems'] as $item) { $collitem = ['field_name' => 'field_collitem']; $collitem['field_target'] = array('value' => $item['target']); if ($item['annotation']) { $collitem['field_annotation'] = $item['annotation']; } $fcitem = FieldCollectionItem::create($collitem); $fcitem->setHostEntity($node, false); $node->{$fcitem->bundle()}[] = array('field_collection_item' => $fcitem); $fcitem->save(true); } $node->save(); return $node; }
/** * Submit callback to remove an item from the field UI multiple wrapper. * * When a remove button is submitted, we need to find the item that it * referenced and delete it. Since field UI has the deltas as a straight * unbroken array key, we have to renumber everything down. Since we do this * we *also* need to move all the deltas around in the $form_state->values * and $form_state input so that user changed values follow. This is a bit * of a complicated process. */ public static function removeSubmit($form, FormStateInterface $form_state) { $button = $form_state->getTriggeringElement(); $delta = $button['#delta']; // Where in the form we'll find the parent element. $address = array_slice($button['#array_parents'], 0, -4); $address_state = array_slice($button['#parents'], 0, -3); // Go one level up in the form, to the widgets container. $parent_element = NestedArray::getValue($form, array_merge($address, array('widget'))); $field_name = $parent_element['#field_name']; $parents = $parent_element['#field_parents']; $field_state = static::getWidgetState($parents, $field_name, $form_state); // Go ahead and renumber everything from our delta to the last // item down one. This will overwrite the item being removed. for ($i = $delta; $i <= $field_state['items_count']; $i++) { $old_element_address = array_merge($address, array('widget', $i + 1)); $old_element_state_address = array_merge($address_state, array($i + 1)); $new_element_state_address = array_merge($address_state, array($i)); $moving_element = NestedArray::getValue($form, $old_element_address); $moving_element_value = NestedArray::getValue($form_state->getValues(), $old_element_state_address); $moving_element_input = NestedArray::getValue($form_state->getUserInput(), $old_element_state_address); // Tell the element where it's being moved to. $moving_element['#parents'] = $new_element_state_address; // Move the element around. $form_state->setValueForElement($moving_element, $moving_element_value); $user_input = $form_state->getUserInput(); NestedArray::setValue($user_input, $moving_element['#parents'], $moving_element_input); $form_state->setUserInput($user_input); // Move the entity in our saved state. if (isset($field_state['field_collection_item'][$i + 1])) { $field_state['field_collection_item'][$i] = $field_state['field_collection_item'][$i + 1]; } else { unset($field_state['field_collection_item'][$i]); } } // Replace the deleted entity with an empty one. This helps to ensure that // trying to add a new entity won't ressurect a deleted entity from the // trash bin. $count = count($field_state['field_collection_item']); $field_state['field_collection_item'][$count] = FieldCollectionItem::create(['field_name' => $field_name]); // Then remove the last item. But we must not go negative. if ($field_state['items_count'] > 0) { $field_state['items_count']--; } // Fix the weights. Field UI lets the weights be in a range of // (-1 * item_count) to (item_count). This means that when we remove one, // the range shrinks; weights outside of that range then get set to // the first item in the select by the browser, floating them to the top. // We use a brute force method because we lost weights on both ends // and if the user has moved things around, we have to cascade because // if I have items weight weights 3 and 4, and I change 4 to 3 but leave // the 3, the order of the two 3s now is undefined and may not match what // the user had selected. $input = NestedArray::getValue($form_state->getUserInput(), $address); // Sort by weight. uasort($input, '_field_collection_sort_items_helper'); // Reweight everything in the correct order. $weight = -1 * $field_state['items_count']; foreach ($input as $key => $item) { if ($item) { $input[$key]['_weight'] = $weight++; } } $user_input = $form_state->getUserInput(); NestedArray::setValue($user_input, $address, $input); $form_state->setUserInput($user_input); static::setWidgetState($parents, $field_name, $form_state, $field_state); $form_state->setRebuild(); }
private function content_ajax_page_fc($fcid) { $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $target = WG::entity_get_field_value($fcitem, 'field_target'); preg_match('%^([^:]+)://([0-9]+)$%', $target, $m); $itemtype = $m[1]; $id = $m[2]; switch ($itemtype) { case 'public318': $identifier = $id; $text = WG::entity_get_field_formatted_text($fcitem, 'field_annotation'); $stylename = 'large'; $icon_uri = _expo_public318_get_icon_uri($identifier); $icontag = WG::render_styled_image($icon_uri, $stylename); $output = '<div class="sticky-fc-public318">' . '<div class="collicon">' . _expo_coll_url($identifier, $icontag) . '</div>' . '<div class="colltext">' . $text . '</div>' . '</div>'; break; case 'storynode': $nid = $id; $story = node_load($nid); $v = entity_view($story, 'ajaxpage'); $output = render($v); break; default: $tag = "<div class=\"sticky\" id=\"sticky_{$pos}\">" . $itemtype . $pos . "</div>"; } $build = ['#markup' => $output]; return $build; }
public function content($nid) { $node = \Drupal::entityManager()->getStorage('node')->load($nid); $fields = $node->getFieldDefinitions(); $fcArray = []; foreach ($fields as $field){ $type = $field->getType(); if ($type == 'field_collection') { $field_collection = $field->get('field_name'); foreach ($node->$field_collection as $key => $item){ $item = $item->value; // $fcArray[$key]; $fc = FieldCollectionItem::load($item); foreach ($fc as $fckey => $field){ $fcArray[$key][$fckey] = $field->value; if (is_object($field[0]) && $field[0]->height){ $image = File::load($field[0]->target_id); $fcArray[$key][$fckey] = str_replace('public://', '/drupal/sites/default/files/', $image->uri->value); $fcArray[$key]['field_mosaic_image_alt'] = $field[0]->alt; } if (is_object($field[0]) && $field[0]->uri){ $fcArray[$key][$fckey] = str_replace('internal:', '', $field[0]->uri); } } } } } return new JsonResponse($fcArray); }
public function getFieldCollectionItem($create = FALSE) { if (isset($this->field_collection_item)) { return $this->field_collection_item; } elseif (isset($this->value)) { // By default always load the default revision, so caches get used. $field_collection_item = FieldCollectionItem::load($this->value); if ($field_collection_item !== NULL && $field_collection_item->getRevisionId() != $this->revision_id) { // A non-default revision is a referenced, so load this one. $field_collection_item = \Drupal::entityTypeManager()->getStorage('field_collection_item')->loadRevision($this->revision_id); } return $field_collection_item; } elseif ($create) { $field_collection_item = FieldCollectionItem::create(['field_name' => $this->getFieldDefinition()->getName()]); // TODO: Uncomment or delete /* $field_collection_item->setHostEntity($this->getEntity(), FALSE); */ return $field_collection_item; } return FALSE; }
/** * Checks access to the operation on the field collection item's host. * * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * * TODO: Document params * * @return string * A \Drupal\Core\Access\AccessInterface constant value. */ public function access(Route $route, AccountInterface $account, $field_collection_item_revision = NULL, FieldCollectionItem $field_collection_item = NULL) { $operation = $route->getRequirement('_access_field_collection_item_host'); return AccessResult::allowedIf($field_collection_item && $field_collection_item->getHost()->access($operation, $account))->cachePerPermissions(); }
/** * Make sure the basic UI and access checks are working. */ public function testBasicUI() { $node = $this->drupalCreateNode(array('type' => 'article')); // Login with new user that has no privileges. $user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($user); // Make sure access is denied. $path = "field_collection_item/add/field_test_collection/node/{$node->id()}"; $this->drupalGet($path); $this->assertText(t('Access denied'), 'Access has been denied.'); // Login with new user that has basic edit rights. $user_privileged = $this->drupalCreateUser(['access content', 'edit any article content']); $this->drupalLogin($user_privileged); // Test field collection item add form. $this->drupalGet('admin/structure/types/manage/article/display'); $this->drupalGet("node/{$node->id()}"); $this->assertLinkByHref($path, 0, 'Add link is shown.'); $this->drupalGet($path); $this->assertText(t($this->inner_field_definition['label'])); $edit = array("{$this->inner_field_name}[0][value]" => rand()); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText(t('Successfully added a @field.', array('@field' => $this->field_collection_name))); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); $field_collection_item = FieldCollectionItem::load(1); // Test field collection item edit form. $edit["{$this->inner_field_name}[0][value]"] = rand(); $this->drupalPostForm('field_collection_item/1/edit', $edit, t('Save')); $this->assertText(t('Successfully edited @field.', array('@field' => $field_collection_item->label()))); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); $this->drupalGet('field_collection_item/1'); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); }
public function content_public318edit($id) { $entity = node_load($id); $r = ''; if ($entity) { $form = ['items' => array()]; $cnt = count($entity->field_collitem); for ($i = 0; $i < $cnt; $i++) { $fcid = $entity->field_collitem[$i]->value; $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $target = WG::entity_get_field_value($fcitem, 'field_target'); $annotation = WG::entity_get_field_value($fcitem, 'field_annotation'); extract(_expo_extract_collitem_target($target)); $identifier = $id; //$collitems[] = ['target'=>$target, 'annotation'=>$annotation]; $delta = microtime(); $delta = preg_replace('%[^0-9]%', '', $delta); $form['items'][$delta] = ['#type' => 'container', '#prefix' => '<li><div class="collitem-element-wrapper" id="collitem-index-' . $delta . '">', '#suffix' => '</div></li>']; $this->_collitem_element_make2($form['items'][$delta], $delta, $identifier, $fcid, $annotation); } $r .= render($form); } echo $r; die; }