foldershare-8.x-1.2/src/Entity/FolderShareTraits/GetSetParentTrait.php
src/Entity/FolderShareTraits/GetSetParentTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; /** * Get/set FolderShare entity parent field. * * This trait includes get and set methods for FolderShare entity parent field. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait GetSetParentTrait { /*--------------------------------------------------------------------- * * Parent field. * *---------------------------------------------------------------------*/ /** * Clears the parent folder ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() in order for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @see ::setParentFolderId() */ private function clearParentFolderId() { $this ->parentid->setValue([ 'target_id' => NULL]); } /** * {@inheritdoc} */ public function getParentFolder() { $id = $this ->getParentFolderId(); if ( $id < 0) { return NULL; } return self::load( $id ); } /** * {@inheritdoc} */ public function getParentFolderId() { $parentField = $this ->parentid->getValue(); if (( empty ( $parentField ) === TRUE) || (isset( $parentField [0]) === FALSE) || ( empty ( $parentField [0]) === TRUE) || (isset( $parentField [0][ 'target_id' ]) === FALSE) || ( empty ( $parentField [0][ 'target_id' ]) === TRUE)) { // Empty. No parent. This is a root item. // // Freshly loaded entities tend to have empty parent fields for root // items. But entities during construction or after having a parent // ID cleared have non-empty parent fields with empty target IDs. // We need to check all of the possibilities to be sure that this is a // root item that has no parent ID. return self::USER_ROOT_LIST; } return (int) $parentField [0][ 'target_id' ]; } /** * Sets the parent folder ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() in order for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @param int $id * The parent ID for the new parent of this item. The value is * not validated and is expected to be a valid entity ID. If the value * is FolderShareInterface::USER_ROOT_LIST or negative, the parent * folder ID is cleared to NULL, which marks this item as a root item. * * @see ::clearParentFolderId() */ private function setParentFolderId(int $id ) { if ( $id < 0) { $this ->parentid->setValue([ 'target_id' => NULL]); } else { $this ->parentid->setValue([ 'target_id' => $id ]); } } } |